0
votes

I have a format url like this:

NSString *url1 = [NSString stringWithFormat:@"%@",http:abc.com/check?para1= 1,2,3,4,5,6,7,8...,1000&Param2= 1,2,3...,1000&Param3=12:12:12.000&Param4=1.12310&Param5=http:yahoo.com/adsf.html]

Im using

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url1]]; [request setHTTPMethod:@"POST"]; [request setValue:[NSString stringWithFormat:@"%d", [url1 length]] forHTTPHeaderField:@"Content-length"]; [request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection connectionWithRequest:request delegate:self];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"response - %@",responseString);

but I can't send to server, Can you guide me this problem?

2
If you can't communicate with the server directly, try using a computer and a modem, those might be better at the subject.user529758
Sorry, can you say more detail?BlueSky
Can you say what error you're getting...Wain
I can't send to server althought Im using method Post, "url1" limited at 2088 character, I wanna send "url1" to serverBlueSky
Use cURL(command line) or cocoa-rest-client to test if they string is accepted by the serverShams Ahmed

2 Answers

1
votes

I modified your code, please try this :

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *url1       = [NSString stringWithFormat:@"%@", @"para1= 1,2,3,4,5,6,7,8...,1000&Param2= 1,2,3...,1000&Param3=12:12:12.000&Param4=1.12310&Param5=http:yahoo.com/adsf.html"];
NSData *postData     = [url1 dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];    
NSString *postLength = [NSString stringWithFormat:@"%u", [postData length]];
[request setURL:[NSURL URLWithString:@"http:abc.com/check"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"Close" forHTTPHeaderField:@"Connection"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];    
NSData *returnData       = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"response - %@",responseString);

May it can help you.

0
votes

I suspect you're doing it wrong and try put the POST body into the URL.

I deduce that from the way you try to set content-length based on url length.

The post body needs to be the content with request.HTTPBody = dataToPost

the url can't be that long then.


I'd advise to read up on the difference between GET and POST

refer to this question to see how to POST data

How send NSData using POST from a iOS application?