0

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.

eglease
  • 2,445
  • 11
  • 18
  • 28
BlueSky
  • 139
  • 3
  • 9

2 Answers2

1

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.

Kuo Ming Lin
  • 143
  • 2
  • 9
  • If you give me your email, I'll give my link url1 for you, because my link is not public, please help me – BlueSky Jun 16 '13 at 12:53
  • now, url1 has fixed the parameter,you can't separate parameter in url1, do you understand me – BlueSky Jun 16 '13 at 12:57
  • No, if you wanna use NSMutableURLRequest to send a POST that you have to separate parameter to set it up with post data and post length. It works for me, I have no idea what it happened ? – Kuo Ming Lin Jun 16 '13 at 13:56
  • Im using server .net c# 4.5. When I send to the server. 4.5 net how to get the value in httpbody? – BlueSky Jun 16 '13 at 16:37
  • Uses Request.Form["fieldName"], example in your code can be Request.Form["Param5"] will get "http:yahoo.com/adsf.html" string value. – Kuo Ming Lin Jun 16 '13 at 16:54
  • are you sure? Im using server .net – BlueSky Jun 16 '13 at 17:09
0

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?

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • I understand difference between GET and POST and I think, I must to using HTTPBody to send data, but url1 with multi parameter was fixed so. Have you any solution? Because if not assigned parameter, url1 is format wrong – BlueSky Jun 16 '13 at 10:16
  • then your code is weird.. you set the POST content-length to the length of the url. – Daij-Djan Jun 16 '13 at 12:54