as the title says, can't get it to work...
Nothing strange about the code:
NSMutableURLRequest *request = [self prepareRequest:inUrl];
[request addValue:[NSString stringWithFormat:#"%#", note] forHTTPHeaderField:NOTE];
....
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
So, this all works fine, and it arrives at my tomcat java spring service fine. EXCEPT for if the "note" string contains newline characters! looks fine when i debuglog in the client, but on my server the parameter is NULL.
Not sure if i need to urlencode or escape something?
Anybody have any pointers? Thankful for input.
Make sure you use correct mode when you send data to the server.
[request setHTTPMethod:#"POST"];
The default HTTP method is “GET”
Update:
Here is how I would generally send data to the server. The parameters may vary depending on your content type.
NSData* data = [bodyString NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
Related
I have an webservices url which appends with password. When I run the application by giving correct password, the application is running and at the same time when I run the app with wrong credentials. the app is running, I didn't have an idea to get rid out of the issue. My code is here:
NSString *post =[[NSString alloc] initWithFormat:#"password=%#",[password text]];
NSLog(#"PostData: %#",post);
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"http://myexample.com/Accountservice/Secure/ValidateAccess?password=abcd&type=1"]];
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];
NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:post] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request1 delegate:self startImmediately:YES];
if(!connection){
NSLog(#"connection failed");
}
else{
NSLog(#"connection succeeded");
}
If I gave 'abcd' as password connection is successfully is displaying. If I gave wrong password connection successfully is displaying. How can I solve the issue? If I gave wrong password need to display an alert.
This control structure here is the problem:
if(!connection){
NSLog(#"connection failed");
}
else {
NSLog(#"connection succeeded");
}
This doesn't check for a bad connection, this checks whether or not the connection object is equal to 0 (nil). Obviously, having initialized it on the line above, the else statement will always log, giving you the false impression that your connection had succeeded. You should become the connection's delegate, and respond to the appropriate delegate methods instead.
The issue may be with the url you are using
Fire the url in the browser and see what it returnes
try add the url params with single quotes accesscode='abcd'
It is not a good practice to pass the credentials via this way.
You could use POST method with some valid encryption to prevent the security issues that can occur
SOLUTION
Try this
NSString *urlString=#"http://myexample.com/Accountservice/Secure/ValidateAccess?";
self.responseData = [NSMutableData data];
NSString *post =[NSString stringWithFormat:#"username=%#&password=%#",userNameTextField.text,passwordTextField.text];
NSData *postData=[post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection0= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection0 start];
I've been trying to get my iPhone application to talk to a WCF service. My application successfully connects/finds the web service, but, never seems to get a non-error response back. When I use visual studios wcf tester it works fine.
Xml format: <mAccess><user></user><pwd></pwd></mAccess>
Is there something wrong with how I'm structuring my head/body?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:website]];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-type"];
NSString *soapMessage = [[NSString alloc] initWithFormat:#"<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body>
<MobileAccess xmlns=\"http://tempuri.org/\"><mAccess><user>user</user><pwd>pwd</pwd><custID>0</custID></mAccess>
</MobileAccess></SOAP-ENV:Body></SOAP-ENV:Envelope>"];
[request setValue:[NSString stringWithFormat:#"%d",[soapMessage length]] forHTTPHeaderField:#"Content-length"];
[request addValue:#"http://tempuri.org/IProviderDataService/MobileAccess" forHTTPHeaderField:#"Soapaction"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
There is one potential error in your construction of the URL request: The "Content-Length" must be set to the length of the HTTP body. This might be different from [soapMessage length] if non-ASCII characters in the message are converted to UTF-8.
So you should use the length of the data after the conversion:
NSData *bodyData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:#"%d",[bodyData length]] forHTTPHeaderField:#"Content-length"];
[request setHTTPBody:bodyData];
In the past, I created an iPhone app which relies on a sqlite database updated by a php page. When you access the PHP page, it prints out a dump of a sqlite database and the app runs the various queries to update the sqlite database. My previous app worked fine in this manner and would update accordingly. I recently tried to apply the same process to a new app with a much bigger database. I have run into problems.
What I am doing is sending some post variables to a PHP page which recognizes the POST and echoes out the sqlite commands to update the database. The problem is that it takes a few seconds for the page to print the commands. If I have the page print some static text, I get it in my NSLog(#"Response data is going to be ==> %#", result); command. If I have to extract the information from my MySQL database, it takes about 3-5 seconds and then prints (on my normal browser). When I run the app, however, it says that Response data is going to be ==> (null) instead of the commands.
Can anyone shed some light on what might be happening? I know that this works when it doesn't print a lot of stuff and when it is static. The dynamic part makes it return null for some reason.
Help appreciated.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlBase]];
[request setHTTPMethod:#"POST"];
[request setTimeoutInterval:60.0];
NSData *post = [myParameters dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[post length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:post];
[request setHTTPBody:[myParameters dataUsingEncoding:NSUTF8StringEncoding]];
// set headers
NSString *contentType = [NSString stringWithFormat:#"text/plain"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"Response data is going to be ==> %#", result);
try to understand this code..it helps u.
NSString *soapMessage=[NSString stringWithFormat:#" YOUR WEB SERVICE CODE",myTxt1.text,myTxt2.text];
NSLog(#"%#",soapMessage);
//-----------create connection
NSURL *url=[NSURL URLWithString:#"http://your local host webservice url"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];
NSString *msgLength=[NSString stringWithFormat:#"%d",[soapMessage length]];
//----------sending request
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/AutenticateUser" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
//[activityIndicator startAnimating];
if ( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
Methods which take no parameter give correct result as expected. Whereas methods of webservice requiring few parameter returns no relevant result instead show Message.
Im using following code:
NSString *post =[[NSString alloc] initWithFormat:#"param1=%#¶m2=%#¶m3=%#¶m4=%#&pageSize=%#&pageNo=%#",#"",#"81",#"1",#"",#"40",#"1"];
NSURL *url = [NSURL URLWithString:#"www.someurlxyz.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
soapMsg = [soapMsg stringByAppendingString:post];
NSLog(#"value of soap is %#",soapMsg);
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
//postData =[postData stringByAppendingString:];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://www.someurlxyz.com/Search/Methodname?" forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
[post release];
Message (which is getting returned is following).
Server did not recognize the value of HTTP Header SOAPAction: http://www.someurlxyz.com/Search/Methodname?
Might be a problem with the namespaces or you might be calling a wrong method.
Use some third party client like SoapUI to check your webservice, and check whether you creating exact string in your request in iPhone app.
What is the server expecting here?
It looks like you are sending param1=¶m2=81¶m3=1¶m4=&pageSize=40&pageNo=1
Which doesn't look like an actual soap message or even XML.
You don't specify what soapMsg is originally but you're appending the string above to it.
Maybe you intended to format the original soapMgs string?
When I try to send out a POST request to a specific site the string I try to send gets cut off. When I check the length of the string in Xcode it is about 55000 characters long. The amount of characters received on the site is about 4500.
This is my code:
-(IBAction)convert {
NSString *rosterText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML"];
NSString *params = [[NSString alloc] initWithFormat:#"roster=%#", rosterText];
NSString *paramsLength = [NSString stringWithFormat:#"%d", [params length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.thesite/index.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-type"];
[request setValue:paramsLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"length = %#",paramsLength);
[webView loadRequest:request];
[params release];
[request release];
}
I have a form on the site where you can manually insert the string and that all works fine, so seems to me something is going wrong within the encoding of the POST data.
Anybody know whats going wrong here? Just can't seem to figure it out!
Thanks a lot!
I am not sure, what is going wrong, but I'd suggest you to use ASIHTTPRequest.
ASIHTTPRequest is an easy to use
wrapper around the CFNetwork API that
makes some of the more tedious aspects
of communicating with web servers
easier. It is written in Objective-C
and works in both Mac OS X and iPhone
applications.