iPhone上的JSON POST请求(使用HTTPS)

ing*_*.am 11 iphone ssl https json objective-c

我托管了一个WCF服务,我试图在iPhone应用程序中使用它作为JSON POST请求.我打算稍后使用JSON序列化程序,但这是我对请求的要求:

    NSString *jsonRequest = @"{\"username\":\"user\",\"password\":\"letmein\"}";
    NSLog(@"Request: %@", jsonRequest);

    NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    [NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
Run Code Online (Sandbox Code Playgroud)

在收到的数据中,我只是将其打印到控制台:

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSMutableData *d = [[NSMutableData data] retain];
    [d appendData:data];

    NSString *a = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];

    NSLog(@"Data: %@", a);
}
Run Code Online (Sandbox Code Playgroud)

在这个回复中,我得到了这样的信息:

Data: <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>
        <!-- I've took this out as there's lots of it and it's not relevant! -->
    </style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="https://mydomain.com/Method/help">service help page</a> for constructing valid requests to the service.</p>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样,我哪里出错了?

我已经阅读过有关使用ASIHTTPPost但我无法让这个演示在iOS4中运行:(

谢谢

编辑:

我刚刚习惯CharlesProxy来嗅出我从iPhone(模拟器)打电话时发生的事情,这就是它给了我的东西: 替代文字

我注意到的唯一奇怪的是它说"未为此主机启用SSL代理:在代理设置,SSL位置启用".有谁知道这意味着什么?(这也发生在我工作的Windows客户端).

我刚刚在我的Windows客户端上运行它,唯一不同的是加密的请求和响应文本.我是否遗漏了某些内容以便使用安全的HTTP连接来执行此操作?

新编辑:这适用于非HTTPS请求,例如:http: //maps.googleapis.com/maps/api/geocode/json?address = UK&_sensor = true.所以我想问题是让iPhone通过SSL正确发送POST?

我也在使用这些方法:

- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
        forAuthenticationChallenge:challenge];
    }

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void) connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        return YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

这让我可以做到这一点.如果我不使用它们,我会收到一个错误,说:

此服务器的证书无效.您可能连接到假装为"mydomain.com"的服务器,这可能会使您的机密信息面临风险.

ing*_*.am 12

固定!

更改:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
Run Code Online (Sandbox Code Playgroud)

至:

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Run Code Online (Sandbox Code Playgroud)

您还需要以下两种方法:

- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
        forAuthenticationChallenge:challenge];
    }

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void) connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        return YES;
    }
}
Run Code Online (Sandbox Code Playgroud)