iPhone上的JSON POST请求(使用HTTPS) [英] JSON POST Request on the iPhone (Using HTTPS)

查看:99
本文介绍了iPhone上的JSON POST请求(使用HTTPS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我托管了WCF服务,并且我试图在iPhone应用程序中将其用作JSON POST请求。我打算稍后使用JSON序列化程序,但这是我对请求的要求:

I have a WCF service hosted and I'm trying to use it within an iPhone app as a JSON POST request. I plan on using the JSON serializer later, but this is what I have for the request:

    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];

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

In my data received I'm just printing it to the console:

- (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);
}

在此回复中,我收到此消息:

And within this response I get this message:

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>

有谁知道为什么会发生这种情况以及我哪里出错?

Does anyone know why this happens and where I'm going wrong?

我读过有关使用 ASIHTTPPost 的内容,但我无法获得在iOS4中运行的演示:(

I've read about using ASIHTTPPost instead but I can't get the demo to run in iOS4 :(

谢谢

编辑:

我刚刚用 CharlesProxy 来嗅出我从iPhone打电话时发生的事情(模拟器),这就是它给我的:

I've just used to CharlesProxy to sniff out what's happening when I call from the iPhone (Simulator) and this is what it gave me:

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

The only thing strange I've noticed is it says "SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations". Does anyone know what this means? (This also happens in my working windows client).

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

I've just ran this on my windows client and the only thing that is different is the Request and Response text which is encrypted. Am I missing something in order to use a secure HTTP connection to do this?

新编辑:
这适用于非HTTPS请求,例如: :
http://maps.googleapis。 COM /地图/ API /地理编码/ JSON地址= UK&安培;传感器=真。所以我想问题是让iPhone通过SSL正确发送POST?

NEW This works with a non-HTTPS request such as: http://maps.googleapis.com/maps/api/geocode/json?address=UK&sensor=true. So I guess the problem is getting the iPhone to send the POST properly over SSL?

我也在使用这些方法:

- (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;
    }
}

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

This allows me to get this far. If I don't use them I get an error sent back saying:


此服务器的证书无效。您可能连接到假冒mydomain.com的服务器,这可能会使您的机密信息处于危险之中。

The certificate for this server is invalid. You might be connecting to a server that is pretending to be "mydomain.com" which could put your confidential information at risk.


推荐答案

固定!

更改:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

to:

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

您还需要以下两种方法:

You also need these two methods:

- (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;
    }
}

这篇关于iPhone上的JSON POST请求(使用HTTPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆