Empty body in POST in ASIHTTPRequest

亡梦爱人 提交于 2019-12-22 06:29:39

问题


Basically, I am sending a POST request with an empty data body:

  ASIHTTPRequest *request [ASIHTTPRequest alloc] init];
  [request setURL:[NSURL URLWithString:escapedUrlString]];
  [request setRequestMethod:@"POST"];
  [request addRequestHeader:@"Content-Type" value:@"application/xml"];
  [request startSynchronous];

But i am getting this response each time:

Incorrect NSStringEncoding value 0x0000 detected. Assuming NSStringEncodingASCII. Will stop this compatiblity mapping behavior in the near future.

I am wondering if it's mandatory to set post values.


回答1:


I don't have much experience with the ASIHTTPRequest wrapper but I can see you are initialising it with alloc:init whilst most examples I've seen have a convenience initialiser requestWithURL:(NSURL*)url i.e.

ASIHTTPRequest *request = [AIHTTPRequest requestWithURL:escapedUrlString];

At a guess I'd say this convenience initaliser will also set some of the required variables for your post request to work, including NSStringEnconding.

From the ASIHTTPRequest documentation at http://allseeing-i.com/ASIHTTPRequest/How-to-use#handling_text_encodings

Handling text encodings

ASIHTTPRequest will attempt to read the text encoding of the received data from the Content-Type header. If it finds a text encoding, it will set responseEncoding to the appropriate NSStringEncoding. If it does not find a text encoding in the header, it will use the value of defaultResponseEncoding (this defaults to NSISOLatin1StringEncoding).

When you call [request responseString], ASIHTTPRequest will attempt to create a string from the data it received, using responseEncoding as the source encoding.




回答2:


As Rog says, use initWithURL or requestWithURL. Anyway, in my case the problem was that I was connecting to a slow server and the request timed out giving the "Incorrect NSStringEncoding value 0x0000 detected" error.

I solved it with:

request.timeOutSeconds = 50;

You can try with more than 50 second if your server is even slower.



来源:https://stackoverflow.com/questions/4356192/empty-body-in-post-in-asihttprequest

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