简体   繁体   English

iOS-格式网址发送到服务器

[英]IOS - Format Url send to Server

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] 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]]; NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url1]]; [request setHTTPMethod:@"POST"]; [request setHTTPMethod:@“ POST”]; [request setValue:[NSString stringWithFormat:@"%d", [url1 length]] forHTTPHeaderField:@"Content-length"]; [对于HTTPHeaderField:@“ Content-length”,[请求setValue:[NSString stringWithFormat:@“%d”,[url1长度]]]; [request setValue:@"text/plain" forHTTPHeaderField:@"Content-type"]; [为HTTPHeaderField:@“ Content-type”请求setValue:@“ text / plain”];

  [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? 但我无法发送到服务器,您能指导我这个问题吗?

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. 愿它可以为您服务。

I suspect you're doing it wrong and try put the POST body into the URL. 我怀疑您做错了,请尝试将POST正文放入URL。

I deduce that from the way you try to set content-length based on url length. 我从尝试基于URL长度设置content-length的方式推断出这一点。

The post body needs to be the content with request.HTTPBody = dataToPost 帖子正文必须是request.HTTPBody = dataToPost的内容

the url can't be that long then. 网址不能那么长。


I'd advise to read up on the difference between GET and POST 我建议仔细阅读GET和POST之间的区别

refer to this question to see how to POST data 请参阅此问题以了解如何发布数据

How send NSData using POST from a iOS application? 如何使用POST从iOS应用程序发送NSData?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM