填表C#&放大器;邮政错误 [英] Fill Form C# & Post Error

查看:88
本文介绍了填表C#&放大器;邮政错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个问题填表格在C#中的一个例子,但是当我运行代码,我刚刚得到的页面回来(因为如果我没有提交任何东西)。我手动尝试将URL并添加变量=值&放大器;变量2 =值2 ,但似乎并没有预先填充表单,不知道这是为什么不工作。

 私人无效button2_Click(对象发件人,EventArgs五)
{
变种编码=新ASCIIEncoding();
VAR POSTDATA =的appid = 001;
POSTDATA + =(与& email=chris@test.com);
POSTDATA + =(与&收到=测试);
POSTDATA + =(与&设备ID = 219481142226.1);
字节[]数据= encoding.GetBytes(POSTDATA);

变种myRequest =
(HttpWebRequest的)WebRequest.Create(http://www.example.com/licensing/check.php);
myRequest.Method =POST;
myRequest.ContentType =应用/的X WWW的形式,进行了urlencoded
myRequest.ContentLength = data.Length;
方通VAR = myRequest.GetRequestStream();
newStream.Write(数据,0,data.Length);
newStream.Close();

VAR响应= myRequest.GetResponse();
VAR responseStream = response.GetResponseStream();
变种responseReader =新的StreamReader(responseStream);
label2.Text = responseReader.ReadToEnd();
}


解决方案

要编程方式提交表单,总体思路是要模拟一个浏览器。要做到这一点,你需要找到的网址,HTTP标头的正确组合,和POST数据,以满足服务器。



要弄清楚什么服务器需要最简单的方法就是使用Fiddler,萤火,或其他工具,它可以让你检查什么的浏览器发送通过线路。然后,您可以通过添加或删除标题,改变POST数据,等等,直到该服务器接受请求在你的代码进行试验。



下面是你可能会遇到一些陷阱:




  • 第一,要确保你提交的形式向正确的目标URL。许多形式不发布到自己,但将发布到不同的网址

  • 接下来,形式可能是检查的会话cookie或身份验证Cookie,这意味着你需要做一个请求(拿起的cookie),然后进行后续的Cookie请求提交表单。

  • 的形式可能隐藏你忘了填写领域。使用Fiddler或Firebug的看提交的表单字段,当你在表单手工填写在浏览器中,并确保包括在你的代码相同的字段

  • 根据服务器实现,可能需要进行编码 @ 字符作为 40%



有可能是太等挑战,但上面是最有可能。要查看完​​整列表,看看我的回答另一个屏幕抓取问题。



顺便说一句,你使用提交表单的代码比需要的更难和详细。相反,你可以使用 WebClient.UploadValues​​() 并完成同样的事情用更少的代码,并自动为你完成编码。像这样的:

 的NameValueCollection POSTDATA =新的NameValueCollection(); 
postData.Add(APPID,001);
postData.Add(电子邮件,chris@test.com);
postData.Add(回执,试);
postData.Add(设备ID,219481142226.1);
postData.Add(checkit,checkit);

WebClient的WC =新的WebClient();
字节[] =结果wc.UploadValues​​(
http://www.example.com/licensing/check.php,
POSTDATA);
label2.Text = Encoding.ASCII.GetString(结果);

更新:



由于我们在评论中讨论,你正在运行到的问题是我在上面提到最初的原因之一:




的形式可能会被检查的会话cookie或认证
饼干,这意味着你需要做一个请求(拿起
饼干),然后进行后续的Cookie请求提交表单。




在使用cookie的会话跟踪或认证,如果请求显示出来没有附加一个cookie的服务器,服务器通常会重定向到相同的URL。重定向将包含设置Cookie 头,这意味着当重定向的URL重新申请,将有客户端连接的cookie。如果第一请求是表单POST,因为服务器和/或客户机不处理在POST重定向这种方式断裂。



解决方法是,正如我最初描述,作出初步的GET请求,拿起饼干,然后进行POST作为第二个请求,回传的cookie。



这样的:

 ; 

公共类CookieAwareWebClient:System.Net.WebClient
{
私人System.Net.CookieContainer饼干=新System.Net.CookieContainer();

保护覆盖System.Net.WebRequest GetWebRequest(URI地址)
{
System.Net.WebRequest请求= base.GetWebRequest(地址);
如果(请求System.Net.HttpWebRequest)
{
VAR HWR =请求为System.Net.HttpWebRequest;
hwr.CookieContainer =饼干;
}
返回请求;
}
}

类节目
{
静态无效的主要(字串[] args)
{
VAR POSTDATA =新System.Collections.Specialized.NameValueCollection();
postData.Add(APPID,001);
postData.Add(电子邮件,chris@test.com);
postData.Add(回执,试);
postData.Add(设备ID,219481142226.1);
postData.Add(checkit,checkit);

变种WC =新CookieAwareWebClient();
字符串URL =htt​​p://www.example.com/licensing/check.php;

//访问该页面,一旦得到连接到此会话cookie的。
// PHP将重定向请求,以确保该Cookie附加
wc.DownloadString(URL);

//现在我们有一个有效的会话cookie,上传表单数据
字节[] =结果wc.UploadValues​​(URL,POSTDATA);
字符串文本= System.Text.Encoding.ASCII.GetString(结果);
Console.WriteLine(文本);
}
}


I took an example of filling in a form in C# from another question, but when I run the code I just get the page back again (as if i didn't submit anything). I tried manually going to the URL and added ?variable=value&variable2=value2 but that didn't seem to pre-populate the form, not sure if that is why this isn't working.

private void button2_Click(object sender, EventArgs e)
{
    var encoding = new ASCIIEncoding();
    var postData = "appid=001";
    postData += ("&email=chris@test.com");
    postData += ("&receipt=testing");
    postData += ("&machineid=219481142226.1");
    byte[] data = encoding.GetBytes(postData);

    var myRequest =
       (HttpWebRequest)WebRequest.Create("http://www.example.com/licensing/check.php");
    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);
    label2.Text = responseReader.ReadToEnd();
}

解决方案

To submit a form programmatically, the general idea is that you want to impersonate a browser. To do this, you'll need to find the right combination of URL, HTTP headers, and POST data to satisfy the server.

The easiest way to figure out what the server needs is to use Fiddler, FireBug, or another tool which lets you examine exactly what the browser is sending over the wire. Then you can experiment in your code by adding or removing headers, changing POST data, etc. until the server accepts the request.

Here's a few gotchas you may run into:

  • first, make sure you're submitting the form to the right target URL. Many forms don't post to themselves but will post to a different URL
  • next, the form might be checking for a session cookie or authentication cookie, meaning you'll need to make one request (to pick up the cookie) and then make a subsequent cookied request to submit the form.
  • the form may have hidden fields you forgot to fill in. use Fiddler or Firebug to look at the form fields submitted when you fill in the form manually in the browser, and make sure to include the same fields in your code
  • depending on the server implementation, you may need to encode the @ character as %40

There may be other challenges too, but those above are the most likely. To see the full list, take a look at my answer to another screen-scraping question.

BTW, the code you're using to submit the form is much harder and verbose than needed. Instead you can use WebClient.UploadValues() and accomplish the same thing with less code and with the encoding done automatically for you. Like this:

NameValueCollection postData = new NameValueCollection();
postData.Add ("appid","001");
postData.Add ("email","chris@test.com");
postData.Add ("receipt","testing");
postData.Add ("machineid","219481142226.1");
postData.Add ("checkit","checkit");

WebClient wc = new WebClient();
byte[] results = wc.UploadValues (
                       "http://www.example.com/licensing/check.php", 
                       postData);
label2.Text = Encoding.ASCII.GetString(results); 

UPDATE:

Given our discussion in the comments, the problem you're running into is one of the causes I originally noted above:

the form might be checking for a session cookie or authentication cookie, meaning you'll need to make one request (to pick up the cookie) and then make a subsequent cookied request to submit the form.

On a server that uses cookies for session tracking or authentication, if a request shows up without a cookie attached, the server will usually redirect to the same URL. The redirect will contain a Set-Cookie header, meaning when the redirected URL is re-requested, it will have a cookie attached by the client. This approach breaks if the first request is a form POST, because the server and/or the client isn't handling redirection of the POST.

The fix is, as I originally described, make an initial GET request to pick up the cookie, then make your POST as a second request, passing back the cookie.

Like this:

using System;

public class CookieAwareWebClient : System.Net.WebClient
{
    private System.Net.CookieContainer Cookies = new System.Net.CookieContainer();

    protected override System.Net.WebRequest GetWebRequest(Uri address)
    {
        System.Net.WebRequest request = base.GetWebRequest(address);
        if (request is System.Net.HttpWebRequest)
        {
            var hwr = request as System.Net.HttpWebRequest;
            hwr.CookieContainer = Cookies;
        }
        return request;
    }
} 

class Program
{
    static void Main(string[] args)
    {
        var postData = new System.Collections.Specialized.NameValueCollection();
        postData.Add("appid", "001");
        postData.Add("email", "chris@test.com");
        postData.Add("receipt", "testing");
        postData.Add("machineid", "219481142226.1");
        postData.Add("checkit","checkit");

        var wc = new CookieAwareWebClient();
        string url = "http://www.example.com/licensing/check.php";

        // visit the page once to get the cookie attached to this session. 
        // PHP will redirect the request to ensure that the cookie is attached
        wc.DownloadString(url);

        // now that we have a valid session cookie, upload the form data
        byte[] results = wc.UploadValues(url, postData);
        string text = System.Text.Encoding.ASCII.GetString(results);
        Console.WriteLine(text);
    }
}

这篇关于填表C#&放大器;邮政错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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