簡體   English   中英

iOS-Https POST的形式仍被識別為JSON Content-Type

[英]iOS - Https POST with Forms Still Recognised As JSON Content-Type

我正在使用以下代碼使用內容類型,形式的參數執行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 / x-www-form-urlencoded,它也指出內容類型為application / json。

這是服務器的響應:

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";
} 

"Content-Type" = "application/json;charset=UTF-8"; 是響應內容類型而不是請求。

您應該檢查JSON響應,以了解為什么會出現400錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM