How to set NSMuteableURLRequest connection to "Connection: close"? - connection

I have tried setting setting my NSMuteableURLRequest to close , but its always on 'keep-alive' , how can I force to close connection. I am doing like this :
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url ];
[req setHTTPBody: [postStr dataUsingEncoding:NSUTF8StringEncoding]];
[req setValue:#"close" forHTTPHeaderField:#"Connection"];
And how bad it is if connections stays on 'keep-alive' ?
Thanks for your help.

Related

SOAP web API request returns HTML instead of XML

I'm new to SOAP API.
I'm using the following code to access my API.
NSString *stringURL=[NSString stringWithFormat:#"http://tennispro.co.in/services.asmx"];
// Allocate and initialize an NSURLConnection with the given request and delegate. The asynchronous URL load process is started as part of this method.
NSString *soapMessage = [NSString stringWithFormat:#"<?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>"
"<ReturnSyncMasterData xmlns=\"http://tempuri.org/\">"
"<datetime>%#</datetime>"
"</ReturnSyncMasterData>"
"</soap:Body>"
"</soap:Envelope>",#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
[request addValue:#"text/html; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}
But in response I get HTML instead of XML. I want to access ReturnSyncMasterData soap action, but I'm unable to call it.
set content type to text/xml
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:#"2159" forHTTPHeaderField:#"Content-Length"];
[request addValue:#"private, max-age=0" forHTTPHeaderField:#"Cache-Control"];
[request addValue:#"gzip" forHTTPHeaderField:#"Content-Encoding"];
[request addValue:#"http://tempuri.org/services/ReturnSyncMasterData" forHTTPHeaderField:#"SOAPAction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:#"POST"];
theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if (theConnection) {
receivedData = [[NSMutableData alloc] init];
}

How to upload image to redmine server?

I tried sending the details(referring this link http://www.redmine.org/projects/redmine/wiki/Rest_api) using the below code(the url is mylocalst:portnumber/uploads.json).But I am getting the 500 error message.What is the meaning of request body as file content While uploading the image to server?Can anyone please provide me some information regarding this?
` NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[dataObj length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
request.timeoutInterval = 60;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/octet-stream" forHTTPHeaderField:#"CONTENT_TYPE"];
[request setValue:#"my api key" forHTTPHeaderField:#"X-Redmine-API-Key"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:dataObj];
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
receivedData = [[NSMutableData alloc] init] ;
}
Thanks in advance

Posting to a Google Forms form using NSURLRequest

I am trying to post a string to a Google Form using NSURLRequest. The method seems to be getting called, but the post to the Google Form doesn't appear. This is the code I am using:
NSURL *requestURL = [NSURL URLWithString:#"https://docs.google.com/forms/d/1T9M6B_k4tiQcSPP5iS2jyU7DCkoRC1jFVql_eQDy9ek/formResponse"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request setHTTPMethod:#"POST"];
NSString *postString = #"entry.757751040=Test sent from iOS App";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
if (connection){
NSLog(#"Connecting...");
}
I created this form solely for testing purposes. "Connecting..." gets logged, so I know the connection is being made, but the string does not appear in the Google Forms Spreadsheet. Am I doing something wrong? Any help would be greatly appreciated.
This piece of code only checks if connection is not nil
if (connection){
NSLog(#"Connecting...");
}
is equivalent to
if (connection != nil){
NSLog(#"Connecting...");
}
As such, it has nothing to do with an actual connection to the URL.
The method initWithRequest:(NSURLRequest *)request delegate:(id)delegate which you are using will perform the request immediately. This means that you have to perform the operations in another order if you want your params to work:
NSURL *requestURL = [NSURL URLWithString:#"https://docs.google.com/forms/d/1T9M6B_k4tiQcSPP5iS2jyU7DCkoRC1jFVql_eQDy9ek/formResponse"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *postString = #"entry.757751040=Test sent from iOS App";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Along with this, consider implementing NSURLConnectionDelegate.

Http GET and POST method in ios

i need to post my value to the test WebView i try'd but my value didnt post and no change while iam running
NSString *post = [NSString stringWithFormat:#"643c30f73a"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"XXXXX.com"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
Are you implementing the required delegate methods for NSURLConnection?
See the documentation at https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html for details and example implementations.
Implementing the delegate methods will at least give you an indication of any error that is occurring.
its worked for me,
NSURL *url = [NSURL URLWithString: #"http://your_url.com"];
NSString *body = [NSString stringWithFormat: #"arg1=%#&arg2=%#", #"val1",#"val2"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];

How to make a PUT HTTP request in ios sdk?

In my application I need to make a PUT type request. I have followed the below steps
NSString *path = #"http://hostname/api/Account/VerifyUser?applicationToken=72B648C6-B2B7-45ED-BA23-2E8AAA187D2C&emailID=xxx#gmail.com&password=aaaaa";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:path]];
[request setHTTPMethod:#"PUT"];
[request setValue:[NSString stringWithFormat:#"%d", string.length] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
For POST and GET I am getting correct output.
I have searched a lot but i am not getting any solution.Please suggest me any possible solution .
Thanks in advance
Try this
For xml request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/form-data" forHTTPHeaderField:#"Content-Type"];
OR
For json request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/json" forHTTPHeaderField:#"Content-Type"];

Resources