【问题标题】:iOS - Https POST with Forms Still Recognised As JSON Content-TypeiOS - 带有仍被识别为 JSON 内容类型的表单的 Https POST
【发布时间】: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 我编辑了我的问题。

标签: ios post https


【解决方案1】:

"Content-Type" = "application/json;charset=UTF-8"; 是响应内容类型而不是请求。

您应该检查 JSON 响应以了解您收到 400 错误的原因。

【讨论】:

  • 我明白了,谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-02-18
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
  • 2017-07-11
  • 2018-02-24
相关资源
最近更新 更多