【发布时间】:2016-10-26 03:05:58
【问题描述】:
我正在使用以下代码执行带有内容类型参数的 https POST,表单。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_IHEARTRADIO_API, URL_USER_LOGIN]]];
[request setHTTPMethod:@"POST"];
NSError *error;
NSString *dataString = @"key1=value1&key2=value2";
NSData *data = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[@([dataString length]) stringValue] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:dataString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
但是,我总是从服务器收到错误代码 400 的响应。另外,它还指出内容类型是 application/json,即使我将其设置为 application/x-www-form-urlencoded。
这是来自服务器的响应:
status code: 400, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Headers" = "X-hostName, X-User-Id, X-Session-Id, Content-Type";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Type" = "application/json;charset=UTF-8";
Date = "Thu, 23 Jun 2016 16:48:11 GMT";
Server = "Apache-Coyote/1.1";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding, X-hostName, X-Accept";
Via = "1.1 varnish";
"X-Cache" = MISS;
"X-Cache-Hits" = 0;
"X-Served-By" = "cache-sjc3643-SJC";
"X-Timer" = "S1466700491.006144,VS0,VE75";
}
【问题讨论】:
-
能不能显示一下web服务代码
-
@Shubhank 我编辑了我的问题。