次のようなc#で記述されたWebサービスがあります。
[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);
}
}
メソッド hello world を呼び出すと。xml 形式で回答を受け取ります。それはjsonにあるはずです。
2 番目のメソッド「helloparam」を呼び出すと、エラーが発生します: パラメータがありません。
iPhone と asp.net Web サービス間の接続と通信を作成する方法の簡単な例を教えてください。前もって感謝します