如何填写表格,并与Web客户端在C#提交 [英] How to fill forms and submit with Webclient in C#

查看:123
本文介绍了如何填写表格,并与Web客户端在C#提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是在用库WebClient的新的Htt presponse和Htt的prequest在C#中,所以我裸露了,如果我的问题是混淆阅读。

Hey, I'm new at using the the libraries WebClient, HttpResponse and HttpRequest in C#, so bare over with me, if my question is confusing to read.

我需要基于C#可以打开一个URL,它被固定的基本授权建立一个WinForm。我这样做与添加这头,是这样的:

I need to build a WinForm based on C# which can open a URL, which is secured with the basic authorization. I did this with adding this to the header, like this:

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

所以票价,这么好!现在我想填写的表格以数字,我觉得从网站源码 - code和发现,名称为数字。所以我写这篇文章:

So fare, so good! Now I would like to fill a form with a number, and I find the source-code from the site, and discover that the name is "number". So I write this:

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

但我怎么提交呢?我会想收到我的结果搜索...

But how do I submit this? I will like to receive my "result-search"...

推荐答案

您或许应该使用的 的HttpWebRequest 这一点。这里有一个简单的例子:

You should probably be using HttpWebRequest for this. Here's a simple example:

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();

这篇关于如何填写表格,并与Web客户端在C#提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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