我在C#中使用WebClient、HttpResponse和HttpRequest库是个新手,所以如果我的问题读起来令人困惑,请耐心听我说。
我需要建立一个基于C#的WinForm,可以打开网址,这是安全的基本授权。我是这样做的,将这个添加到头文件中,如下所示:
using (WebClient wc = new WebClient())
{
wc.Headers.Add(HttpRequestHeader.Authorization, "Basic " +
Convert.ToBase64String(
Encoding.ASCII.GetBytes(username + ":" + password)));
}
到现在为止还好!现在我想用一个数字填写一个表单,我从网站上找到了源代码,然后发现它的名字是" number“。所以我写了这个:
NameValueCollection formData = new NameValueCollection();
formData["number"] = number
byte[] responseBytes = wc.UploadValues(theurl, "POST", formData);
string response = Encoding.ASCII.GetString(responseBytes);
textBox_HTML.Text = response;
但是我该如何提交呢?我想收到我的“搜索结果”...
发布于 2009-04-27 14:34:33
为此,您可能应该使用HttpWebRequest
。下面是一个简单的例子:
var strId = UserId_TextBox.Text;
var strName = Name_TextBox.Text;
var encoding=new ASCIIEncoding();
var postData="userid="+strId;
postData += ("&username="+strName);
byte[] data = encoding.GetBytes(postData);
var myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();
var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
var result = responseReader.ReadToEnd();
responseReader.Close();
response.Close();
发布于 2012-04-27 18:44:20
试试这个:
using System.Net;
using System.Collections.Specialized;
NameValueCollection values = new NameValueCollection();
values.Add("TextBox1", "value1");
values.Add("TextBox2", "value2");
values.Add("TextBox3", "value3");
string Url = urlvalue.ToLower();
using (WebClient client = new WebClient())
{
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] result = client.UploadValues(Url, "POST", values);
string ResultAuthTicket = System.Text.Encoding.UTF8.GetString(result);
}
发布于 2009-04-29 06:15:08
我已经找到了解决我的问题的办法。首先,我对http通信的一些基础知识感到困惑。这是由我编写的一个python脚本引起的,该脚本具有不同的通信方法。
我通过从头开始生成后期数据并打开包含在form-action中的uri解决了这个问题。
https://stackoverflow.com/questions/793755
复制相似问题