ASIHTTPRequest Post request fails while works via HTTPClient

血红的双手。 提交于 2019-12-11 10:01:52

问题


As stated in title, I send a POST request using ASIHTTPRequest and it fails, but the same POST request works in HTTPClient, am I doing something wrong?

- (void)postData:(NSData *)postData {
    //...
    NSMutableData *mutableData = [postData.mutableCopy autorelease];
    [request setRequestMethod:@"POST"];
    [request setPostBody:mutableData];
    [request setPostLength:mutableData.length];
    //....
}

回答1:


That's not quite enough in all cases, at least I also had to add the correct Content-Type header to the request:

[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
[request appendPostData:[theString dataUsingEncoding:NSUTF8StringEncoding]]



回答2:


OK I found the problem, for some reason I cannot do

[request setPostBody]

I have to use

[request appendPostData:[theString dataUsingEncoding:NSUTF8StringEncoding]]

instead.



来源:https://stackoverflow.com/questions/14210222/asihttprequest-post-request-fails-while-works-via-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!