0

我有一个 Web 服务,用 c# 编写,如下所示:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloParam(string param)
    {
        return "Hello " + param;
    }

我在 Xcode 中的代码如下所示:

 - (IBAction)getButton:(id)sender {

    NSString *urlString = @"http://172.16.43.132/Server/Service1.asmx/HelloParam";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod: @"POST"];
   // [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"charset=utf-8" forHTTPHeaderField:@"Content-Type"];
       NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
       if (errorReturned) 
    {
        NSLog(@"Shit");
    }
    else 
    {
        NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", retVal);


    }
}

当我调用方法你好世界。我收到 xml 格式的答案。它应该在 json 中。

当我调用第二种方法“helloparam”时,我得到一个错误:参数丢失。

你能给我一个简单的例子来说明如何在 iPhone 和 asp.net 网络服务之间建立连接和通信吗?提前致谢

4

0 回答 0