I have URL which takes a parameter. In Firefox. I have download add-on and find the parameter for the request. I found PostData Tab on the httpFox add-on tool and I saw the parameter and value.
I created a string and added the parameter, and encoded that string value
NSString *parameterId = [NSString StringWithFormat:#"ac$2_sdrex_es34_4333f=Caa111"];
NSString EncodeID = [parameterId
stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
...and created the PostData,
PostData = [encodeID dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
...and set the postLength,
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
...and set the http header values,
[request setValue:postLength forHttpHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
and then make the connection,
connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
But this results in the following error...
Error Domain = NSURLErrorDomain code= -1002 unsupported url
I have encoded the URL too.
Can anyone advise me how to resolve this issue?
This is what after creating the postData!
NSURL *url = [NSURL URLWithString:urlString];
theRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[theRequest setValue:#"win-eqjf1b1ep06:20000" forHTTPHeaderField:#"Host"];
[theRequest setValue:#"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10" forHTTPHeaderField:#"User-Agent"];
[theRequest setValue:#"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"en-us,en;q=0.5" forHTTPHeaderField:#"Accept-Language"];
[theRequest setValue:#"gzip,deflate" forHTTPHeaderField:#"Accept-Encoding"];
[theRequest setValue:#"ISO-8859-1,utf-8;q=0.7,*;q=0.7" forHTTPHeaderField:#"Accept-Charset"];
[theRequest setValue:#"115" forHTTPHeaderField:#"Keep-Alive"];
[theRequest setValue:#"keep-alive" forHTTPHeaderField:#"Connection"];
[theRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody:postData];
#all
Thanks in advance
NSMutableURLRequest only supports http://, https://, ftp://, or file://
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i
Are you using some other protocol?
Related
I'm trying to connect to a server which requires a username and password for Authentication. My code is the following:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"username:password" forHTTPHeaderField:#"Authorization"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
But this does not work, I always get an error message:
title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
When I try to connect to a server which doesn't nee an Authentication this code code works great. So, how to set the username and password correctly?
You should specify the domain of server, try this format:
NSString *authen = [NSString stringWithFormat:#"%#\\%#:%#", m_domain,
m_account, m_password];
[request setValue:authen forHTTPHeaderField:#"Authorization"];
this work for me!
When i try to parse special char like "care&love", it just shows "care", the part after "&" truncated How can i make it to show the complete string???
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSString *postJson = [NSString stringWithFormat:#"&json=%#", jsonRequest];
NSData *requestData = [postJson dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
Assuming that we are speaking about the value of jsonRequest, you have to URL encode that string before passing it to the postJson, otherwise & is seen as parameter separator.
json=care&love is interpreted as json="care" and love="" by the receiving server.
Url encoding the value will look like json=love%26care and will be decoded by the server as json="love&care"
use stringByAddingPercentEscapesUsingEncoding for escaping, see at: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAddingPercentEscapesUsingEncoding:
You could use Cocoa's built-in JSON serialization. This would rule out improper string escaping (which is most likely the problem in your current code).
Sample:
NSError* error = nil;
NSDictionary* jsonDict = #{#"json": #"EL- STR 261: HAUL & SPOT"};
NSData* requestData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
I am using objective C JSON parsing library. My web service returns the JSON response. My parser fails as there is an '\' character in the response string. The response string is like ":\/\/68.491.5.780\/iphoneapplication" but I want to be like "://68.491.5.780/".
My code is here:
NSURL *url=[NSURL URLWithString:
#"http:// url address/Accountservice/Security/ValidateAccess?accesscode=abcd&type=0"];
NSData *postData =
[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
So how can I remove all back slash "\" in the response receive from web service?
you can replace \ with whitespace like:-
NSString *str =[NSString stringWithFormat:#"%#",#" hi \ hoowoer"];
str = [str stringByReplacingOccurrencesOfString:#"\""
withString:#""];
NSLog(#"==%#",str);
OUTPUT
hi hoowoer
I'm trying to do a simple POST request to a server, with this code:
NSString *post = [[NSString alloc] initWithFormat:#"email=%#&password=%#", self.email.text, ..]; // .. simplified keychainItem
NSData *postEncoded = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:#"%d", [postEncoded length]];
NSURL *url = [NSURL URLWithString:#"http://eng.studev.groept.be/web2.0/a11_web02/testApp.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postEncoded];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
When debugging on the server, this actually results in a GET request. That explains why I'm getting a PHP error when trying to read $_POST[ ] variables. Bottom line: why isn't the setHTTPMethod: being accepted?
(extra information: when just coding in PHP on the server, use of POST works normally)
i have the next code:
NSURL *url = [NSURL URLWithString:#"http:...."];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [""soapMessage"" length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [""soapMessage"" dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:20];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
Then sometimes, when i click a lot of times (Just to try where can fail the app) in my interface in the buttons assigned to start a new connection, the connection doesn't give a reply (I send the request but looks like is blocked after).
I would like to know what is the use of "setTimeoutInterval"? AND if there are some ways to limit the time to wait one response?
Thanks!
How do you know that the request started in the first place? You might be waiting for a response for a request that never started.
It appears you are not testing for theConnection being set to nil. also theRequest and url could also be nil if you run out of memory.