NSURLConnection NSMutableURLRequest 和 HTTPS

发布于 2024-10-19 14:02:36 字数 1810 浏览 1 评论 0原文

我有以下代码,可将图像上传到网站。

[request setURL:nsurl];
[request setHTTPMethod:@"POST"];  

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];  
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];  
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];  

/* 
 now lets create the body of the post 
 */  
NSMutableData *body = [NSMutableData data];  

//parameter1  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"guid\"\r\n\r\n%@", [Settings sharedInstance].uploadID] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
//Image  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filMyFile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[NSData dataWithData:dataObj]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

// setting the body of the post to the reqeust  
[request setHTTPBody:body];  

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

通过 HTTP 运行时一切正常,但当我切换到 HTTPS 时,它会失败并显示

错误:Error Domain=NSURLErrorDomain Code=-1005“网络连接丢失。” UserInfo=0x1c7f70 {NSErrorFailingURLStringKey=https:.......

与之通信的服务器是 MS Server 2008 R2。

任何想法将不胜感激。

尼尔.

I have the following code which uploads an image to a web site.

[request setURL:nsurl];
[request setHTTPMethod:@"POST"];  

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];  
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];  
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];  

/* 
 now lets create the body of the post 
 */  
NSMutableData *body = [NSMutableData data];  

//parameter1  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"guid\"\r\n\r\n%@", [Settings sharedInstance].uploadID] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
//Image  
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filMyFile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithString:@"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[NSData dataWithData:dataObj]];  
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

// setting the body of the post to the reqeust  
[request setHTTPBody:body];  

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

All is well when running over HTTP, but when I switch to HTTPS it fails with

Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x1c7f70 {NSErrorFailingURLStringKey=https:.......

The server it is communicating with is MS Server 2008 R2.

Any ideas would be appreciated.

Neil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文