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?
Related
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = [jsonString base64EncodedString];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#wsName",baseUrl]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:encodedString forHTTPHeaderField:#"Data"];
This doesn't make sense. You're creating JSON, base-64 encoding it (you never have to base-64 encode JSON; if you were really trying to encrypt it, you have to come up with something better), and setting the header Data with this, while simultaneously providing a Content-Type informing the server that the request was XML, even though it wasn't.
If the server was expecting JSON, you'd just send JSON:
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#wsName",baseUrl]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue: #"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:jsonData];
If the server was expecting XML, you'd use the text/xml or application/xml setting for Content-Type, but you'd also (a) have to construct the XML content; and (b) supply that to setHTTPBody.
If you wanted to secure the request, you'd use https://.
Bottom line, you must confirm what precisely the server is looking for. Don't guess, but rather look at the server's source code or documentation or talk to the developers. But your client-side code sample is unlikely to work as it stands.
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];
I want to send user picture and user's other information to server using POST method from my iphone application.
my content type is as below
NSString *boundary = [NSString stringWithString:#"--"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
but it is posting only image. my other data is not posting on server.
And if I use below content type then
[ request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
then it is sending other data only. not sending image.
Please suggest me way by which I can send both image and other info.
Encode the image using Base64 like this
[Base64Coder encodeData:UIImagePNGRepresentation(image)];
You can download Base64 class from this link.
Edited: Added Code sample
NSString *reqStr = [NSString stringWithFormat:#"iPhone_request=<game><name>%#</name><image>%#</image></game>",txtGameName.text, [Base64Coder encodeData:UIImagePNGRepresentation(imgGame.image)]];
// Replace occurrences of + with %2B
reqStr = [[reqStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
reqData = [reqStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:reqData];
Use ASIHTTPRequest its simple and easy to use :)
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"];
I have to make a HTTPS Post with some XML data, but I need to send the XML in the HTTP header.
Using the body I would do something like this
request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CACHE_POLICY timeoutInterval:TIMEOUT_INTERVAL];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postData];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
But this does not work for the header, anyone know how to make this?
The XML is something like this
<request>
<code1>666</code1 >
<code2>656</code2 >
<code3>767</code3 >
</request >
Thanks.
[request setValue:#"<request><code1>666</code1 ><code2>656</code2 ><code3>767</code3 ></request >" forHTTPHeaderField:#"Your header field name?"];
Or do you mean "in the URL", rather than a random header? HTTP Headers aren't for bunging random data into, in ordinary circumstances.
Off the top of my head, without trying to compile (no Xcode with me right now), excuse typos:
NSMutableString *urlString = [NSMutableString string];
[urlString append:#"http://wahtever.com?something=something"];
[urlString append:#"&data="];
[urlString append:[#"<request>...etc</request>" stringByAddingPercentEscapesUsingEncoding:UTF8StringEncoding];
NSURL *url = [NSURL URLwithString:urlString];
request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CACHE_POLICY timeoutInterval:TIMEOUT_INTERVAL];