【问题标题】:iphone upload picture https postiphone上传图片https帖子
【发布时间】:2012-01-26 14:14:05
【问题描述】:

我正在尝试使用 post 方法上传带有 https 的图片。

我已经知道如何将图片上传到 http 服务器。

要将图片上传为https,我只需要在http上添加一个s吗?

非常感谢

例如:

NSString *urlString = @"http://192.168.10.30:8080/thek_save/JSP/file_upload.jsp";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

【问题讨论】:

    标签: iphone https upload


    【解决方案1】:

    您可以使用相同的代码在 POST 上将图片上传到 HTTP。只需将“HTTP://”替换为“HTTPS://”即可。 您应该在 HTTPS 连接上安装了有效的 SSL 证书

    【讨论】:

      【解决方案2】:

      请阅读此代码...阅读此代码后您知道“如何使用post connection”...表示通过post connection上传到图像并使用delegte方法

      -(void) sendImageToServer
      {
      
              NSURL *postURL = [NSURL URLWithString:[NSString   stringWithFormat:@"@"http://192.168.10.30:8080/thek_save/JSP/file_upload.jsp";]]; 
      
              NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
                                                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                     timeoutInterval:30.0];
      
              // change type to POST (default is GET)
              [postRequest setHTTPMethod:@"POST"];
      
              NSMutableData *postBody = [NSMutableData data];
      
      
              // just some random text that will never occur in the body
              NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
      
              // header value
              NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
      
              // set header
              [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
      
      
              // image ------------
      
              [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
              [postBody appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
              [postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
              [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
              // add it to body
      
      
              NSData *data = UIImageJPEGRepresentation(img, 90);
      
              [postBody appendData:data];
              [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
      
          /// UIImage *testImage = [UIImage imageWithData:data];
      
      
              // final boundary
              [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
      
      
              // add body to post
              [postRequest setHTTPBody:postBody];
      
              NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
      
              if( theConnection )
              {
                  webData = [NSMutableData data] ;
              }
              else
              {
                  NSLog(@"theConnection is NULL");
              }
      
          }
      
      ///Use Delegete
      #pragma mark -
      #pragma mark ConnectionDelegate
      -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      {
          [webData setLength: 0];
      }
      -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
      {
          [webData appendData:data];
      }
      -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {
          NSLog(@"ERROR with theConenction");
      
      }
      -(void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
          NSLog(@"DONE. Received Bytes: %d", [webData length]);
          NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
          NSLog(@"%@",theXML);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-10
        • 2015-08-09
        • 1970-01-01
        • 2018-12-30
        • 1970-01-01
        • 2020-08-27
        相关资源
        最近更新 更多