如何在C#中使用Web客户端发布数据,以特定的URL [英] How to post data to specific URL using WebClient in C#

查看:98
本文介绍了如何在C#中使用Web客户端发布数据,以特定的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用HTTP POST与Web客户端的一些数据发布到我有一个特定的网址。

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

现在,我知道这可以用WebRequest的完成,但由于某种原因,我想使用Web客户端来代替。那可能吗?如果是这样,有人可以告诉我一些示例或点我朝着正确的方向?

Now, I know this can be accomplished with WebRequest but for some reasons I wanna use WebClient instead. Is that possible? If so, can someone show me some example or point me to the right direction?

推荐答案

我刚刚找到了解决方案,并推倒它比我想象的容易:)

I just found the solution and yea it was easier than I thought :)

所以这里是解决方案:

string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
}

它就像魅力:)

这篇关于如何在C#中使用Web客户端发布数据,以特定的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆