我托管了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(模拟器)打电话时发生的事情,这就是它给我的:
我注意到的唯一奇怪的是它说“未为此主机启用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”的服务器,这可能会使您的机密信息面临风险。
答案 0 :(得分:12)
固定!
更改:
[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;
}
}
答案 1 :(得分:3)
我根本不了解Objective C,但尝试将Accept标头设置为application/json
而不是application/jsonrequest
。
答案 2 :(得分:0)
变化
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
到
NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
答案 3 :(得分:0)
dic是带有对象和键的NSMutable Dictionary。 baseUrl是您的服务器webservice url。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *encodedString = [jsonString base64EncodedString];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@wsName",baseUrl]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:encodedString forHTTPHeaderField:@"Data"];