问题
I'm trying to call a simple JSON webservice with a parameter in objective c. Doesn't work so far.
Here is the web service method:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void LogIn(string username, string password)
{
Context.Response.Write(username + "___" + password);
Context.Response.End();
}
Here is my Objective C code:
// Build dictionnary with parameters
NSString *username = @"usernameTest";
NSString *password = @"passwordTest";
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:username forKey:@"username"];
[dictionnary setObject:password forKey:@"password"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
options:kNilOptions
error:&error];
NSString *urlString = @"http://localhost:8080/ListrWS.asmx/LogIn";
NSURL *url = [NSURL URLWithString:urlString];
// Prepare the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonData];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if (errorReturned)
{
//...handle the error
}
else
{
NSString *retVal = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@", retVal);
}
Here is what:
NSLog(@"%@", retVal);
display:
{"Message":"Thread was being aborted.","StackTrace":" at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)\r\n
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)\r\n at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)\r\n
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at
System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.Threading.ThreadAbortException"}
Any ideas ?
回答1:
I had a similar problem. I was setting the HTML body with jsonData as you do and it didn't work. It turned out that the JSON service wasn't configured as it was supposed to be.
So, instead of setting the HTML body, try calling the URL like a GET method.
I mean, remove the lines that you set the HTML body, and change the URL to
http://localhost:8080/ListrWS.asmx/LogIn?username=usernameTest&password=passwordTest
Don't change the method (POST).
If this works, you will have to do some work on the server side.
回答2:
For the iOS part,
Your code looks OK. There should be no problem on the client side. But you can do the following debuggings;
- Just before you call
sendSynchronousRequest:returningResponse:errorinsert a breakpoint and check if your jsonData is valid. Because you are not checking theerroryou assigned for JSON Serialization. - If there is no problem with your JSON data, find another basic JSON server and try consuming it. If your client-side works, you will know there is something wrong on your server-side.
Cheers
回答3:
in the node of web.config add the next lines:
<system.web>
<webServices>
<protocols>
<add name="HttpPost"/>
</protocols>
</webServices>
http://support.microsoft.com/default.aspx?scid=kb;en-us;819267
来源:https://stackoverflow.com/questions/9966614/calling-json-web-service-with-parameters-objective-c-ios