我想使用WebClient执行QueryString
但使用POST方法
这就是我到目前为止所得到的
CODE:
using (var client = new WebClient())
{
client.QueryString.Add("somedata", "value");
client.DownloadString("uri");
}
它正在运行,但不幸的是它使用GET而不是POST,我希望它使用POST的原因是我正在进行网络报废,这就是我在WireShark中看到的请求。 [它使用POST作为一种方法但不仅仅是查询字符的后期数据。]
答案 0 :(得分:1)
这对您有帮助,请使用WebRequest
代替WebClient
。
using System;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
class ThreadTest
{
static void Main()
{
WebRequest req = WebRequest.Create("http://www.yourDomain.com/search");
req.Proxy = null;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string reqString = "searchtextbox=webclient&searchmode=simple";
byte[] reqData = Encoding.UTF8.GetBytes(reqString);
req.ContentLength = reqData.Length;
using (Stream reqStream = req.GetRequestStream())
reqStream.Write(reqData, 0, reqData.Length);
using (WebResponse res = req.GetResponse())
using (Stream resSteam = res.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
File.WriteAllText("SearchResults.html", sr.ReadToEnd());
System.Diagnostics.Process.Start("SearchResults.html");
}
}
答案 1 :(得分:1)
回答您的具体问题:
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] response = client.UploadData("your url", "POST", new byte[] { });
//get the response as a string and do something with it...
string s = System.Text.Encoding.Default.GetString(response);
但是使用WebClient可以是PITA,因为它不接受cookie,也不允许你设置超时。