问题
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