iOS应用程序通过GET方法连接到.NET Web Service

时间:2012-03-02 14:12:18

标签: ios web-services get

大家好!我的英语很差,很抱歉。

我想在我的测试iOS应用程序中实现一个函数。

有一个.NET Webservice API,就像

一样

“https://xxx.xxx.xx.xxx/FMS/Pages/Service/FMService.svc/Login”

我想用两个参数连接API:user和pass

使用GET方法,网址如下:

“https://xxx.xxx.xx.xxx/FMS/Pages/Service/FMService.svc/Login?user=xxx&pass=xxx”

如果登录,Webservice将返回JSON值,就像{“d”:“success”}

如果没有,它还将返回JSON值,如{“d”:“failure”}

我正在使用ASIHTTPRequest框架和JSON框架

我不知道如何实现这个功能。所以请帮助我,非常感谢。

祝福!

NSURL *url = [NSURL URLWithString:@"https://192.168.1.245/FMS/Pages/Service/FMService.svc/Login?user=jiangxd&pass=123456"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request addRequestHeader:@"Accept" value:@"application/json"];

[request addRequestHeader:@"Content-Type" value:@"application/json"];

[request setRequestMethod:@"GET"];

[request setDelegate:self];

[request startAsynchronous];

NSString *responseString = [request responseString];

NSDictionary *responseDict = [responseString JSONValue];

NSString *unlockCode = [responseDict objectForKey:@"d"];

NSLog(@"%@",unlockCode);

unlockCode总是为空......我不明白为什么!

NSURL *url = [NSURL URLWithString:@"https://192.168.1.245/FMS/Pages/Service/FMService.svc/Login?user=jiangxd&pass=123456"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) 
{
    NSString *responseString = [request responseString];
    NSDictionary *responseDict = [responseString JSONValue];
    NSString *unlockCode = [responseDict objectForKey:@"d"];
    NSLog(@"%@",unlockCode);
}
else
{
    NSLog(@"%@",[error description]);
}

现在我将startAsynchronous更改为startSynchronous,但也有错误:

错误域= ASIHTTPRequestErrorDomain代码= 1“发生连接失败:SSL问题(可能的原因可能包括错误/过期/自签名证书,时钟设置为错误日期)”UserInfo = 0x6b81640 {NSUnderlyingError = 0x6b811b0 “操作无法完成。(OSStatus error -9807。)”,NSLocalizedDescription =发生连接失败:SSL问题(可能的原因可能包括错误/过期/自签名证书,时钟设置为错误日期)} < /强>

注意:网址为https,而不是http!(这是我收到错误的原因吗?)

但是如果我直接使用浏览器访问url,Webservice将返回正确的值...

谢谢!

1 个答案:

答案 0 :(得分:0)

您应该尝试一下,然后发布您的代码。你要求有人为你写这整个功能,我认为这不是这个网站的目的。

其次,ASIHTTPRequest背后的开发人员不再支持它。除非社区接受,否则您可能只想学习如何从头开始进行NSURLConnections。

编辑:我们走了。所以,你正在异步地这样做,这意味着当你启动它时,你不会立即得到响应。您是否设置了回调来处理响应?

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}