2013-10-07 71 views
2

我要消耗Web服務有一個方法,像這樣的文件..上傳HttpPostedFileBase以及一些參數

我必須通過此方法提交我的數據並在執行 此方法後保存webservice響應。所有這些操作都會在點擊按鈕時執行。我怎樣才能做到這一切。

我使用.NET Framework 4.5和MVC 4

注意:我只是結束web服務的用戶

UPDATE1:SubmitUser是一個Web服務端的方法和我的網址是

somepage.com/api/SubmitUser

回答

1

你可以使用一個HttpClient,它可以發送一個multipart/form-data編碼請求:

byte[] imageData = ... 
var requestContent = new MultipartFormDataContent(); 
var imageContent = new ByteArrayContent(imageData); 
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg"); 

// Add the image 
requestContent.Add(imageContent, "image", "image.jpg"); 

// Now add some additional parameters that will be bound to the UserReg object 
requestContent.Add(new StringContent(HttpUtility.UrlEncode("value1")), "param1"); 
requestContent.Add(new StringContent(HttpUtility.UrlEncode("value2")), "param2"); 
requestContent.Add(new StringContent(HttpUtility.UrlEncode("value3")), "param2.subparam1"); 

var client = new HttpClient(); 
var res = client.PostAsync("http://your_web_service_endpoint", requestContent).Result; 
+0

什麼將在'imageData'圖像路徑或'system.drawing.Image'對象或字節轉換圖像中。 – user2408194

+0

它必須是一個字節數組。所以如果你已經有一個'System.Drawing.Image',你需要先將它轉換爲一個字節數組。 –

+0

對不起,我不記得web服務的響應將返回在json合成器中,請告訴我一點我如何能從json得到它的響應 – user2408194

相關問題