如何在 C# 中使用 WebClient 将数据发布到特定 URL [英] How to post data to specific URL using WebClient in C#

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

问题描述

我需要使用HTTP Post"使用 WebClient 将一些数据发布到我拥有的特定 URL.

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

现在,我知道这可以通过 WebRequest 完成,但出于某些原因,我想改用 WebClient.那可能吗?如果是这样,有人可以向我展示一些示例或指出正确的方向吗?

Now, I know this can be accomplished with WebRequest but for some reasons I want to 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# 中使用 WebClient 将数据发布到特定 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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