Empty body in POST in ASIHTTPRequest

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 get this answer every time:

Invalid NSStringEncoding value 0x0000. Assume NSStringEncodingASCII. This compatibility mapping behavior will stop soon.

I am wondering if message values ​​need to be set.

+6
objective-c iphone
source share
2 answers

I don't have much experience with the ASIHTTPRequest wrapper, but I see that you initialize it with alloc:init , while most of the examples I saw have the convenience initializer requestWithURL:(NSURL*)url ie

 ASIHTTPRequest *request = [AIHTTPRequest requestWithURL:escapedUrlString]; 

I guess I would say that this initaliser convenience will also set some of the necessary variables for your mail request, including NSStringEnconding .

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

Text Encoding Processing

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

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

+9
source

As Rog says, use initWithURL or requestWithURL. In any case, in my case, the problem was that I was connecting to a slow server and the request timeout raised the error "NSStringEncoding 0x0000 was not detected correctly".

I solved this with:

 request.timeOutSeconds = 50; 

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

+3
source

All Articles