gpt4 book ai didi

ios - 同步 HTTPS POST 请求 iOS

转载 作者:可可西里 更新时间:2023-11-01 04:25:12 25 4
gpt4 key购买 nike

对于 Android,我可以通过以下方式发送 POST 请求:

HttpClient http = new DefaultHttpClient();
HttpPost request = new HttpPost("https://somewebsite.com");
request.setEntity(new StringEntity(data));
http.execute(request);

但是,在 iOS 上我收到以下错误:

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)

在 iOS 上使用 https 执行同步 POST 请求的最佳方式是什么?

最佳答案

你可以试试这个功能:

-(NSData *)post:(NSString *)postString url:(NSString*)urlString{

//Response data object
NSData *returnData = [[NSData alloc]init];

//Build the Request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

//Send the Request
returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

//Get the Result of Request
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];

bool debug = YES;

if (debug && response) {
NSLog(@"Response >>>> %@",response);
}


return returnData;
}

下面是你如何使用它:

NSString *postString = [NSString stringWithFormat:@"param=%@",param];
NSString *urlString = @"https://www.yourapi.com";

NSData *returnData = [self post:postString url:urlString];

编辑:

我在源码中找到错误代码:

errSSLHostNameMismatch = -9843,/* 对端主机名不匹配 */

问题应该出在您的服务器上。

这里来自docs :

errSSLHostNameMismatch -9843 The host name you connected with does not match any of the host names allowed by the certificate. This is commonly caused by an incorrect value for the kCFStreamSSLPeerName property within the dictionary associated with the stream’s kCFStreamPropertySSLSettings key. Available in OS X v10.4 and later.

希望对你有帮助

关于ios - 同步 HTTPS POST 请求 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25335168/

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