我正在尝试使用目标c中的参数调用简单的JSON Web服务。到目前为止不起作用。
以下是Web服务方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void LogIn(string username, string password)
{
Context.Response.Write(username + "___" + password);
Context.Response.End();
}
这是我的Objective C代码:
// 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);
}
以下是:
NSLog(@"%@", retVal);
显示:
{"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"}
有什么想法吗?
答案 0 :(得分:2)
我有类似的问题。我正在使用jsonData设置HTML正文,但它不起作用。事实证明,JSON服务没有按照预期配置。
因此,不要设置HTML正文,而是尝试像GET方法一样调用URL。
我的意思是,删除您设置HTML正文的行,并将网址更改为
http://localhost:8080/ListrWS.asmx/LogIn?username=usernameTest&password=passwordTest
请勿更改方法(POST)。
如果这样做,你将不得不在服务器端做一些工作。
答案 1 :(得分:0)
对于iOS部分,
您的代码看起来不错。客户端应该没有问题。但是你可以进行以下调试;
sendSynchronousRequest:returningResponse:error
之前插入一个断点并检查您的jsonData是否有效。因为您没有检查为JSON序列化分配的error
。干杯
答案 2 :(得分:0)
<system.web>
中添加下一行:
<system.web>
<webServices>
<protocols>
<add name="HttpPost"/>
</protocols>
</webServices>
http://support.microsoft.com/default.aspx?scid=kb;en-us;819267