我有一个托管的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 (Simulator)调用时发生的情况,这是它给我的结果:
我唯一注意到的奇怪事情是它说“此主机未启用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;
}
}
这让我走到了这一步。如果我不使用它们,就会收到一个错误消息:
该服务器的证书无效。您可能正在连接到一个假冒“mydomain.com”的服务器,这可能会危及您的机密信息。