gpt4 book ai didi

ios - HTTP(S) POST : connectionDidFinishLoading gets called with empty data, 可能存在信任问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:20 25 4
gpt4 key购买 nike

我正在尝试与 TSYS 支付网关进行通信,该网关具有来 self 的 iOS 应用程序的 https 服务器。我的原始代码如下。

-(void)postRequest:(NSString *)jsonBody{
NSData *requestData = [NSData dataWithBytes:[jsonBody UTF8String]
length:[jsonBody length]];
NSURL *url = [NSURL URLWithString:TSYS_URL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d",[requestData length]]
forHTTPHeaderField:@"Content-Length"];
[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:requestData];

[[NSURLConnection alloc]initCustomURLWithRequest:request delegate:self ];
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];
NSLog(@"http error code : %d", code);

[self.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"receivedData.length : %d", [self.receivedData length]);
connection = nil;
}

- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error {
connection = nil;
}

支付网关地址为

#define TSYS_URL @"https://stagegw.transnox.com/servlets/TransNox_API_Server"

我在控制台中得到了这个

http error code : 404
receivedData.length : 0

当我看到日志时,第一个调用端口是关于 HTTPS SSL 身份验证的,所以我写了一个 android 项目,通过 trusting all certificate (not ideal, I know) 进行 POST。 ,万岁,我得到了正确的回应。但是,当我删除使 HttpClient 能够信任所有证书的部分时,我得到了空响应(在 Android 中)。

为了让 iOS 信任所有证书,我添加了以下代码

- (BOOL)connection:(NSURLConnection *)connection 
canAuthenticateAgainstProtectionSpace:
(NSURLProtectionSpace *)protectionSpace {
//This always returns true
return [protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:
(NSURLAuthenticationChallenge *)challenge {
if([challenge.protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust]){
NSURLCredential *credential = [NSURLCredential
credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential
forAuthenticationChallenge:challenge];
}
[challenge.sender
continueWithoutCredentialForAuthenticationChallenge:challenge];
}

我仍然得到同样的错误。

好的,我的问题是

  1. 首先,为什么这不起作用?我需要得到这个工作,以便我可以推进项目。自从我的项目处于开始状态,我不介意解决方案是信任所有人证书。我不知何故需要让服务器说话。
  2. 我收到的身份验证质询类型是NSURLAuthenticationMethodServerTrust。理想情况下我应该得到服务器颁发证书并使我的 NSURLConnection 对象信任它。如何从服务器获取证书文件?

最佳答案

我终于让它工作了。最后,这不是身份验证的问题。除了上面的所有代码,我还必须在请求中传递一个 header 字段“User-Agent”。当我通过它时,我开始从服务器获得响应。

关于ios - HTTP(S) POST : connectionDidFinishLoading gets called with empty data, 可能存在信任问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21376292/

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