29

我不熟悉在 C# 中使用库 WebClient、HttpResponse 和 HttpRequest,如果我的问题难以阅读,请耐心等待。

我需要构建一个基于 C# 的 WinForm,它可以打开一个 URL,该 URL 受到基本授权的保护。我通过将其添加到标题中来做到这一点,如下所示:

using (WebClient wc = new WebClient())
{
    wc.Headers.Add(HttpRequestHeader.Authorization, "Basic " +
    Convert.ToBase64String(
    Encoding.ASCII.GetBytes(username + ":" + password)));
}

到目前为止,一切都很好!现在我想用一个数字填写一个表格,我从网站上找到源代码,发现名字是“数字”。所以我写这个:

NameValueCollection formData = new NameValueCollection();  
formData["number"] = number
byte[] responseBytes = wc.UploadValues(theurl, "POST", formData);
string response = Encoding.ASCII.GetString(responseBytes);
textBox_HTML.Text = response; 

但是我该如何提交呢?我想收到我的“搜索结果”...

4

6 回答 6

51

您可能应该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();
于 2009-04-27T14:34:33.243 回答
44

Try this:

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);
}
于 2012-04-27T10:44:19.843 回答
3

我找到了解决我的问题的方法。首先,我对 http 通信中的一些基础知识感到困惑。这是由我编写的 python 脚本引起的,它有不同的通信方法。

我通过从头开始生成 POST 数据并打开包含在表单操作中的 uri 来解决它。

于 2009-04-29T06:15:08.013 回答
1

BFree 的回答效果很好。不过,我要注意的一件事是,数据连接确实应该是 url 编码的,否则你会遇到数据中的“=”和“&”符号之类的问题。

下面是 VB.NET 版本,urlencoded 并支持 UTF-8(请注意,url 编码需要引用 System.Web.dll,它仅在我从 .NET 4 Compact Framework 切换到常规 . NET 4 框架)。

Imports System.Web
Imports System.Net
Imports System.IO

Public Class WebFormSubmitter

    Public Shared Function submit(ByVal address As String,
                                  ByVal values As Dictionary(Of String, String)) As String
        Dim encoding As New UTF8Encoding
        Dim postData As String = getPostData(values:=values)
        Dim data() As Byte = encoding.GetBytes(postData)

        Dim request = CType(WebRequest.Create(address), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length
        Dim newStream = request.GetRequestStream()
        newStream.Write(data, 0, data.Length)
        newStream.Close()

        Dim response = request.GetResponse()
        Dim responseStream = response.GetResponseStream()
        Dim responseReader = New StreamReader(responseStream)
        Return responseReader.ReadToEnd()
    End Function

    Private Shared Function getPostData(ByVal values As Dictionary(Of String, String)) As String
        Dim postDataPairList As New List(Of String)
        For Each anEntry In values
            postDataPairList.Add(anEntry.Key & "=" & HttpUtility.UrlEncode(anEntry.Value))
        Next
        Return String.Join(separator:="&", values:=postDataPairList)
    End Function

End Class
于 2011-11-07T02:29:13.857 回答
1

使用 System.Net.Http.HttpClient 发布表单并将响应读取为字符串:

var formData = new Dictionary<string, string>();
formData.Add("number", number);

var content = new FormUrlEncodedContent(formData);

using (var httpClient = new HttpClient())
{
    var httpResponse = await httpClient.PostAsync(theurl, content);

    var responseString = await httpResponse.Content.ReadAsStringAsync();
}
于 2015-04-18T21:06:32.410 回答
0

您已经使用 提交了它UploadValues。问题是:你的“结果搜索”是什么?页面返回什么?HTML?如果是这样 - HTML Agility Pack是解析 html 的最简单方法。

于 2009-04-27T14:45:06.150 回答