iPhone上的JSON POST请求(使用HTTPS)
我有一个托管的WCF服务,我正试图在iPhone应用程序中使用它作为JSON POST请求。我计划稍后使用JSON序列化程序,但这是我对请求的要求:iPhone上的JSON POST请求(使用HTTPS),iphone,objective-c,json,ssl,https,Iphone,Objective C,Json,Ssl,Https,我有一个托管的WCF服务,我正试图在iPhone应用程序中使用它作为JSON POST请求。我计划稍后使用JSON序列化程序,但这是我对请求的要求: NSString *jsonRequest = @"{\"username\":\"user\",\"password\":\"letmein\"}"; NSLog(@"Request: %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"https://mydom
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>
这让我走了这么远。如果我不使用它们,我会收到一个返回的错误,上面说:
此服务器的证书无效。您可能正在连接假装为“mydomain.com”的服务器,这可能会使您的机密信息处于危险之中
我根本不知道目标C,但尝试将Accept头设置为
application/json
,而不是application/jsonrequest
修复
更改:
[request setValue:@"application/x-www-form-urlencoded" 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;
}
}
改变
到
dic是带有对象和键的可变字典。 baseUrl是您的服务器Web服务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"];
我早就试过了。应该是这样吗?只是又做了一次,我还是得到了同样的回答(
application/json
绝对是json数据的正确MIME类型。请参阅您是否确定您的web服务接受url编码格式的数据并以json格式响应?如果您执行curl命令并在obj中编写相同的程序,这将很容易解决问题。如果您发布源代码,尤其是为您的WCF服务配置。此外,您是否尝试访问错误消息中的服务帮助页?不幸的是,我没有编写该服务。我有一个.NET应用程序使用类似的过程完全按照我的要求运行。因此,我知道这不是服务器问题!如果您的服务器中的日志有任何有趣的内容,您可以查看它们吗?我没有acc现在请告诉他们。我知道该服务正在设置并与其他客户端一起工作。顺便说一句,您没有忘记在最后一个方法的最后一行中“返回否”吗?我不确定,我对一个无效的方法返回“是”。记不得为什么了。@ing0您能将这一行翻译成swift plz[请求设置值:@“application/x-www-form-urlencoded”吗forHTTPHeaderField:@“内容类型”];@QadirHussain抱歉,我没有写太多swift。请查看以下答案:
- (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;
}
}
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
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"];