0

我尝试将捕获的图像从 iPhone 保存到服务器。我完成了 Xcode。像这样的代码

NSMutableURLRequest  *request= [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\" filename=\"Test.png\"\r\n", imgName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];

现在我尝试使用单点触控,

在这里我创建连接,发送请求和接收响应工作正常。

但是这里我不知道如何将 Header & Body 设置为 NSMutableURLRequest,只是我需要知道如何传递参数(如上面的 Xcode)。

NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl("http://url.com"), NSUrlRequestCachePolicy.ReloadRevalidatingCacheData, 20);
request.HttpMethod = "POST";

var connectionDelegate = new TestNSURLConnectionDelegate();
var connection = new NSUrlConnection(request, connectionDelegate);
connection.Start();

任何人都可以帮我解决这个问题...

4

1 回答 1

0

使用 System.Net 访问 HttpWebRequest。

您只需发布到 url.. 并将图像的字节 [] 写入请求的正文。

在我的示例中,ServiceBase 值映射到静态字符串,而“Service.SubmitImage”是指向服务方法端点的静态字符串。

 public void SubmitImage(byte[] image)
    {
        var request = WebRequest.Create(ServiceBase + Service.SubmitImage);
        request.Method = "POST";
        request.ContentType = "application/octet-stream";
        request.ContentLength = image.Length;
        Stream str = request.GetRequestStream();
        str.Write(image, 0, image.Length);
        str.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }
于 2013-08-20T15:41:24.290 回答