ASIHTTPRequest addRequestHeader issue

2019-08-09 05:34发布

问题:

I am using ASIHttpRequest and make use GET method to send header to server. I call method addRequestHeader like this

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"myHeader" value:@"abc"];

It's not working. But if I use NSMutableURLRequest to add header and request to server, it works. I don't know anything wrong when calling addRequestHeader methods for ASIHTTPRequest library. Have anyone seen this issue?

回答1:

Wow ok so, yeah if this is a new app, please do NOT use ASIHttpRequest. It has long been supplanted by the delightful AFNetworking.

If this is an existing application, you really should work on a migration plan off ASI.

However, in an attempt to actually answer your question - that is the appropriate setup per the documentation, and is how I used to use it. My guess is something is broken under the covers and judging from a basic google request, there are issues with iOS 7 including memory leaks and requests just failing.



回答2:

You can do it via NSURLRequest

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.abc.com"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:@"AAA" forHTTPHeaderField:@"Hello-there"];
request = [mutableRequest copy];
NSLog(@"%@", request.allHTTPHeaderFields);

Hope this helps .. :)