I need to change a default HTTP header "Connection" in NSMutableURLRequest to "close" value, instead of "keep-alive". What is the best practice to do it?
I tried to use TCP level and create HTTP request myself + use GCDAsyncSocket. It works, but I think this solution looks not very nice.
Thanks.
As simple as u can think of:
NSMutableURLRequest* request = [NSMutableURLRequest alloc] initWithURL:url];
[request setValue:#"close" forHTTPHeaderField:#"Connection"];
If u want to add value:
[request addValue:VALUE forHTTPHeaderField:#"Connection"];
Related
I'm using ASIHTTPRequest to access a web based API and need to set a header for App authentication. Note that this is not a server level authentication it is at API level. I've tried every thing I could find and most of the answers on the web as well as the ones here at www.stackoverflow.com tell me to use something like:
[request addRequestHeader:#"username" value:#"asdf"];
This does not work for me. The guy who built the API I'm using told me that I need to set the header as:
Authorization: TRUEREST username=PersonName&password=pass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad
So I tried the following:
NSURL *url = [NSURL URLWithString:#"http://ifish-uk.co.uk/rest_users/login.json"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"username" value:#"MyUser"];
[request addRequestHeader:#"password" value:#"MyPass"];
[request addRequestHeader:#"apikey" value:#"dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad"];
But it didn't work... I even tried setting the Request type to GET because the developer told me I should do this:
[request setRequestMethod:#"GET"];
This didn't work... The API developer told me he is made this module as follow:
POST /rest_catches/add.json HTTP/1.1
Host: ifish-uk.co.uk
Authorization: TRUEREST username=MyUser&password=MyPass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad
Cache-Control: no-cache
any help would be greatly appreciated.
You should add only one header is Authorization no need to add separate headers for each field (use, pass, etc).
Fill it with your specific values and send.
[request addRequestHeader:#"Authorization" value:#"TRUEREST username=PersonName&password=pass&apikey=dfiu6aewruif3Bismillah4Rah3anArahimiImi22MyDad"];
Did you try adding:
[request setRequestMethod:#"POST"];
?
I'm working on a mobile share point application. I'm facing issue while trying to retrieve list collection of a sub-site. Went through so many blogs and tried different approaches but of no use.
My Home site has two sub sites (SampleSubsite & SampleSubsite2) in which I've document libraries created.
The GetSite web service defined in /_vti_bin/SiteData.asmx returns the following response.
Url = "http://sp2010.lab.xyz.local:20852";
Url = "http://sp2010.lab.xyz.local:20852/SampleSubsite";
Url = "http://sp2010.lab.xyz.local:20852/SampleSubSite2";
Now, when I try to fetch the List Collection of a sub site (in this case for example SampleSubsite), I'm constructing the URL as follows.
http://sp2010.lab.xyz.local:20852/SampleSubsite/_vti_bin/Lists.asmx
I've tried multiple options but of no use. (Test is the root site name). In all the below 3 cases, I get the root collection itself.
http://sp2010.lab.xyz.local:20852/Sites/SampleSubsite/_vti_bin/Lists.asmx
http://sp2010.lab.xyz.local:20852/Sites/Test/SampleSubsite/_vti_bin/Lists.asmx
http://sp2010.lab.xyz.local:20852/Test/SampleSubSite/_vti_bin/Lists.asmx
Could anyone through some light on it.
Code snippet Below:
#define kFetchListCollectionXML #"<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
#"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"\
#"<soap:Body>"\
#"<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />"\
#"</soap:Body>"\
#"</soap:Envelope>"\
- (void)getListCollectionOfSite:(NSString *)sitePath {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/_vti_bin/Lists.asmx",sitePath]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"http://schemas.microsoft.com/sharepoint/soap/GetListCollection" forHTTPHeaderField:#"SOAPAction"];
[request setValue:[NSString stringWithFormat:#"%ld",(unsigned long)kFetchListCollectionXML.length] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:[kFetchListCollectionXML dataUsingEncoding:NSUTF8StringEncoding]];
[self performConnectionWithRequest:request];
}
The issue worked fine after creating the Alternate Access Mapping and creating the request for the the subsites as follows:
For subsite "SampleSubsite" with URL "http://sp2010.lab.xyz.local:20852/SampleSubsite", the request URL should be constructed as follows.
http://sp2010.lab.xyz.local:20852/SampleSubsite/_vti_bin/Lists.asmx
I was struggling with this using the New-WebServiceProxy cmdlet. The problem went away when I added ?WSDL to the end of the query string, as suggested here http://blogs.msdn.com/b/powershell/archive/2010/06/24/using-new-webserviceproxy-to-get-modify-and-add-items-to-a-list-in-sharepoint-2007.aspx
I'm trying to download a file from a cloud via a GET HTTP request, and in my REST Client (Postman for Chrome) I get the results as expected, but when I pass the request in iOS, I get a NULL response. What could be the problem?
In my REST Client, I pass the URL as http://myserver.com/api/download.json?filepath=/&fileid=document:1pLNAbof_dbXGn43GtMNafABMAr_peTToh6wEkXlab7U&filename=My Info.doc with a header field for a authorization key, and it works perfectly fine.
While in iOS, if I do such a thing:
[request setURL:[NSURL URLWithString:URLString]]; // I tried even to hardcode the
// URLString to be one of the
// working URLs from the Postman
// Client, doesn't work as well.
[request setHTTPMethod:#"GET"];
NSData* response = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponseList
error:&requestErrorList];
response is null, and I get a status code 500.
Anyone know how to make this work? Thanks.
it looks like your URL has a space in it, you'll need to escape that before sending the string to NSURL.
it would also be helpful to know what the full error from your server is if this doesn't work. what does requestErrorList contain?
but if the only problem is the non-escaped characters something like this should work.
NSString *encodedString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[request setURL:[NSURL URLWithString:encodedString];
An HTTP status code 500 occurs when the webservice is not able to handle your request. An internal error occured.
Try to add some more details to your request.
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
I need some tutorial, how to use HTTP POST (or GET) request from my iOS app. I want to send one string to my server and than write it to database. I've found this piece of code:
NSString *post = #"key1=val1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
I add it to UIAction button, but it don't send anything to my server. On my server, I have a PHP script that takes that "key1" from post and than it write it to db.
<?
$postr = $_POST["key1"];
$con0 = mysql_connect("server","db","pass");
mysql_select_db("table", $con0);
mysql_set_charset('utf8',$con0);
mysql_query("INSERT INTO tok (token) VALUES ('$postr')");
mysql_close();
?>
Can anybody tell me what I am doing wrong?
Look at AFnetworking and work through their api. Sending a post request using the api is very fast, and is what most apps use for web connectivity.
Also please dont be discouraged by sarcastic comments. You should tag iOS projects as iOS and not xcode (unless you actually need help with the actual program xcode). But I don't think it's productive to harrass everyone that comes in and makes this mistake. A more tactful way would be a personal message as opposed to a public retort
https://github.com/AFNetworking/AFNetworking
AFNetworking is a very powerful library that can help you to reduce the effort on create your HTTP requests, you can find several examples on how to use this library on its github page.
You may find this answer useful for your needs: AFNetworking Post Request
I have a problem with my NSMutableURLRequest. My server supports both JSON and XML formats and they are separated with an access header. It also defaults to JSON if no access header is set.
Which basically means that when I want the response in XML I need to create a request with 'application/xml' as access header.
The problem I'm facing now is that even if I pass in the correct access header to get the response in XML I still end up with JSON (because that's default). It's like my request disregards the access header. Is there anything else I need to create in order to make my request work with headers?
The request is really simple:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:#"application/xml" forHTTPHeaderField:#"Accept"];
I have confirmed that my request contains my headers by printing allHTTPHeaderFields:
headers: {
Accept = "application/xml";
}
May be use should provide Content-Type?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:#"application/xml" forHTTPHeaderField:#"Content-Type"];
Because "application/xml" is always passed in "Content-Type" header field.