How to post data to specific URL using WebClient in C#

后端 未结 8 1631
深忆病人
深忆病人 2020-11-22 07:12

I need to use \"HTTP Post\" with WebClient to post some data to a specific URL I have.

Now, I know this can be accomplished with WebRequest but for some reasons I wa

8条回答
  •  孤独总比滥情好
    2020-11-22 08:06

    Using WebClient.UploadString or WebClient.UploadData you can POST data to the server easily. I’ll show an example using UploadData, since UploadString is used in the same manner as DownloadString.

    byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
                    System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2") );
     
    string sret = System.Text.Encoding.ASCII.GetString(bret);
    

    More: http://www.daveamenta.com/2008-05/c-webclient-usage/

提交回复
热议问题