When trying to connect to the following web method, “Request format is invalid” is returned:
public string myWebMethod(string test)
{
return "connected";
}
This is the objective C code:
NSString *seshID = @"test-session";
NSString *post = [NSString stringWithFormat:@"test=%@", seshID];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *comparisonURLString = SERVER_COMPARE_URL_STRING;
NSURL *comparisonURL = [NSURL URLWithString: comparisonURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:comparisonURL];
[request setHTTPMethod:@"POST"];
[request addValue:postLength forHTTPHeaderField:@"Content-Length"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(response);
However, if I alter the web method so that it takes no parameters:
public string myWebMethod()
And change the corresponding line in my obj-c code to:
NSString *post = [NSString stringWithFormat:@""];
It works, with the response:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">connected</string>
Can anyone explain why the latter seems to work but the former doesn’t? Thanks.
I’ve got a similar problem between JAVA WebMethod and C#. The problem was resolved by creating a class string1 inherant from SoapHeader and containing a string[].
Then for your example, you can try this
or
You can check too with Fiddler what kind of data is really passed.