iPhone中如何知道服务器回复?

发布于 2024-10-27 09:45:51 字数 2590 浏览 3 评论 0原文

 - (IBAction)  uploadImage   {  
    /*
     turning the image into a NSData object  
     getting the image back out of the UIImageView  
     setting the quality to 100  
     */  
      NSData *imageData = UIImageJPEGRepresentation(image.image, 100);  
      // setting up the URL to post to  
    NSString *urlString = @"http://uploadhere.com/photos/iphone.php";

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html 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];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"q\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"here1");
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    NSLog(@"here2");
    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"here3");
    //NSLog(returnString);
    //picText = rowZero.pickerLabel.text;
    NSLog(@"here4");
    rowZero.path       = returnString;
    //NSLog(rowZero.path);


}

在与网络部分的连接中,这种情况正在发生。

我正在使用此代码将图像上传到服务器上。但是,在此 NSLog(@"%@", returnData) 给我值,但 NSLog(@"%@", returnString) 返回 null 值。我的意思是我怎么知道我的图像是否已上传到服务器上?

 - (IBAction)  uploadImage   {  
    /*
     turning the image into a NSData object  
     getting the image back out of the UIImageView  
     setting the quality to 100  
     */  
      NSData *imageData = UIImageJPEGRepresentation(image.image, 100);  
      // setting up the URL to post to  
    NSString *urlString = @"http://uploadhere.com/photos/iphone.php";

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
     add some header info now
     we always need a boundary when we post a file
     also we need to set the content type

     You might want to generate a random boundary.. this is just the same 
     as my output from wireshark on a valid html 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];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"q\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"here1");
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    NSLog(@"here2");
    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"here3");
    //NSLog(returnString);
    //picText = rowZero.pickerLabel.text;
    NSLog(@"here4");
    rowZero.path       = returnString;
    //NSLog(rowZero.path);


}

In the connection with web section, this is happening.

I am using this code to upload image on server. However, in this NSLog(@"%@", returnData) is giving me the value but NSLog(@"%@", returnString) is returning null value. I mean how would I know that my image is being uploaded on the server or not?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-03 09:45:51

您应该使用 returningResponseerror 来验证请求是否成功。

...
NSURLResponse *response = nil;
NSError *error = nil;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error != nil) {
    // Handle error. Response code can be retrieved from NSURLResponse
}

You should make use of the returningResponse and error to validate that the request was successful.

...
NSURLResponse *response = nil;
NSError *error = nil;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error != nil) {
    // Handle error. Response code can be retrieved from NSURLResponse
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文