2016-04-13 48 views
0

我目前正試圖提交一個包含多個文件到其目標服務器的表單,但不知道如何使用HttpWebRequest發送此表單數據。該表單是MultipartFormDataContent類型。到目前爲止,我已經看到了使用HttpWebRequest提交表單的方法,但是這是通過將表單構造爲字符串並將其轉換爲字節數組來完成的。因此,像這樣,如何在HttpWebRequest中使用MultipartFormDataContent?

string postData = ""; 

     foreach (string key in postParameters.Keys) 
     { 
      postData += HttpUtility.UrlEncode(key) + "=" 
        + HttpUtility.UrlEncode(postParameters[key]) + "&"; 
     } 

     HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 
     myHttpWebRequest.Method = "POST"; 

     byte[] data = Encoding.ASCII.GetBytes(postData); 
Stream requestStream = myHttpWebRequest.GetRequestStream(); 
     requestStream.Write(data, 0, data.Length); 
     requestStream.Close(); 

然而,我的形式不是一個字符串,但類型MultipartFormDataContent的,因此我不知道該怎麼辦。

回答

1

因此,我在這裏做的第一件事是將MultipartFormDataContent對象轉換爲Byte數組。

Byte [] byteArray = await form.ReadAsByteArrayAsync();

然後我把它傳遞給一個函數,它使得HttpWebRequest()。

private string HttpPostRequest(string url, MultipartFormDataContent formData, Byte[] form) 
    { 
     HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 
     myHttpWebRequest.Method = "POST"; 

     myHttpWebRequest.ContentType = formData.Headers.ContentType.ToString(); 
     myHttpWebRequest.ContentLength = formData.Headers.ContentLength.Value; 
     myHttpWebRequest.ProtocolVersion = HttpVersion.Version10; 
     myHttpWebRequest.ServicePoint.ConnectionLimit = 24; 
     myHttpWebRequest.KeepAlive = false; 
     myHttpWebRequest.Timeout = System.Threading.Timeout.Infinite; 
     myHttpWebRequest.AllowWriteStreamBuffering = false; 
     System.Net.ServicePointManager.Expect100Continue = false; 


     Stream requestStream = myHttpWebRequest.GetRequestStream(); 
     int i = 0; 
     attachAttempt: 
     try 
     { 
      requestStream.Write(form, 0, form.Length); 
     } 
     catch (IOException e) 
     { 
      if (i < 5) 
      { 
       i++; 
       goto attachAttempt; 
      } 

      DialogResult attachFail; 
      string attachFailMessage = e.Message.ToString() + " Please check your network and try again."; 
      string attachFailCaption = "Network error."; 
      MessageBoxButtons emailFailButtons = MessageBoxButtons.OK; 
      attachFail = MessageBox.Show(attachFailMessage, attachFailCaption, emailFailButtons, MessageBoxIcon.Error); 

      return ""; 
     } 
     requestStream.Close(); 

     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

     Stream responseStream = myHttpWebResponse.GetResponseStream(); 

     StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default); 

     string pageContent = myStreamReader.ReadToEnd(); 

     myStreamReader.Close(); 
     responseStream.Close(); 

     myHttpWebResponse.Close(); 

     return pageContent; 
    } 

我在研究了與這個領域相關的幾個建議後編譯了這個。你可能會發現你可能不需要在myHttpWebRequest對象上設置一些標誌,但這對我來說是有效的。我還發現,網絡上的問題會嚴重影響這一點,因爲您的主機和目標機器之間的連接可能會中止,並且正因爲如此,我已將Write()放入try和catch塊以防萬一需要輸出一個錯誤。

相關問題