2013-08-20 45 views
0

我嘗試將捕獲的圖像從iPhone保存到服務器。我完成了Xcode。像在monotouch中使用NSMutableURLRequest調用webservice

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]; 

代碼現在我嘗試用MonoTouch的,

我在這裏創建連接,發送請求&接收響應工作正常。

但是在這裏我不知道如何設置標題& 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(); 

任何一個可以幫助我,這...

+0

如果您使用Xarmain,最好的方法是使用標準的C#方法做一個Web服務調用(Web客戶端或HttpWebRequest的),就像您在任何其他.NET程序。 – Jason

回答

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(); 
    }