How to submit a multipart/form-data HTTP POST request from C#

后端 未结 5 1474
予麋鹿
予麋鹿 2020-12-15 20:10

What is the easiest way to submit an HTTP POST request with a multipart/form-data content type from C#? There has to be a better way than building my own request.

T

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 20:50

    The System.Net.WebClient class may be what you are looking for. Check the documentation for WebClient.UploadFile, it should allow you to upload a file to a specified resource via one of the UploadFile overloads. I think this is the method you are looking to use to post the data...

    It can be used like.... note this is just sample code not tested...

    WebClient webClient = new WebClient();

    webClient.UploadFile("http://www.url.com/ReceiveUploadedFile.aspx", "POST", @"c:\myfile.txt");

    Here is the MSDN reference if you are interested.

    http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile.aspx

    Hope this helps.

提交回复
热议问题