gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:23:57 25 4
gpt4 key购买 nike

我托管了一个 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];

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

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

在这个响应中我得到了这个消息:

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>

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

我读过有关使用 ASIHTTPPost 的信息相反,但我无法让演示在 iOS4 中运行:(

谢谢

编辑:

我刚刚习惯了CharlesProxy嗅探当我从 iPhone(模拟器)打电话时发生了什么,这就是它给了我的: alt text

我注意到的唯一奇怪的事情是它说“未为此主机启用 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;
}
}

这让我可以走到这一步。如果我不使用它们,我会收到一条错误消息:

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

到:

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

你还需要这两个方法:

- (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 - iPhone 上的 JSON POST 请求(使用 HTTPS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4085978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com