A fellow emailed me wanting to screen scrape, er, ah, harvest a page that only displays the data he wants with a postback.
一位同事给我发了电子邮件,想筛选一下,嗯,收获一个页面,该页面仅显示他想要的数据并回发。
Remember what an HTTP GET looks like under the covers:
记住一个HTTP GET看起来像在幕后:
GET /whatever/page.aspx?param1=value¶m2=value
GET /whatever/page.aspx?param1=value¶m2=value
Note that the GET includes no HTTP Body. That's important. With a POST the 'DATA' moves from the QueryString into the HTTP Body, but you can still have stuff in the QueryString.
请注意,GET不包含HTTP正文。 那很重要通过POST,“数据”从QueryString移到HTTP正文中,但是您仍然可以在QueryString中包含内容。
POST /whatever/page.aspx?optionalotherparam1=value
Content-Type: application/x-www-form-urlencoded
Content-Length: 25
param1=value¶m2=valuePOST /whatever/page.aspx?optionalotherparam1=value 内容类型:application / x-www-form-urlencoded 内容长度:25 param1 = value&param2 = value
Note the Content-Type header and the Content-Length, those are important.
请注意Content-Type标头和Content-Length,它们很重要。
A POST is just the verb for when you have an HTTP document. A GET implies you got nothing.
POST只是拥有HTTP文档时的动词。 GET表示您一无所获。
So, in C#, here's a GET:
因此,在C#中,这是一个GET:
public static string HttpGet(string URI)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy(ProxyString, true); //true means no proxy
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
公共静态字符串HttpGet(字符串URI)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy =新的System.Net.WebProxy(ProxyString, true ); // true表示没有代理
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr =新的System.IO.StreamReader(resp.GetResponseStream());
返回sr.ReadToEnd()。Trim();
}
Here's a POST:
这是一个POST:
public static string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy(ProxyString, true);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream ();
os.Write (bytes, 0, bytes.Length); //Push it out there
os.Close ();
System.Net.WebResponse resp = req.GetResponse();
if (resp== null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
公共静态字符串HttpPost(字符串URI,字符串参数)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy =新的System.Net.WebProxy(ProxyString, true );
//添加这些,因为我们正在执行POST
req.ContentType = “ application / x-www-form-urlencoded” ;
req.Method = “ POST” ;
//我们需要计算要发送的字节数。 发布的伪造表格应为name = value&
字节[]字节= System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes,0,bytes.Length); //将它推到那里
os.Close();
System.Net.WebResponse resp = req.GetResponse();
如果(resp == null )返回null ;
System.IO.StreamReader sr =新的System.IO.StreamReader(resp.GetResponseStream());
返回sr.ReadToEnd()。Trim();
}
I could and should have put in more 'using' statements, but you get the gist. And, there are other ways to have done this with the BCL, but this is one.
我本可以并且应该添加更多的“使用”语句,但是您的主旨是。 而且,还有其他方法可以通过BCL完成此操作,但这只是其中一种。
Now, how would you fake an HTTP PostBack? Use a tool like ieHttpHeaders to watch what a real postback looks like, and well, fake it. :) Just hope they don't require unique/encrypted ViewState (via ViewStateUserKey or EnableViewStateMac) for that page, or you're out of luck.
现在,您将如何伪造HTTP回发? 使用ieHttpHeaders之类的工具来观察真实回发的样子,然后伪造它。 :)只是希望他们不需要该页面的唯一/加密ViewState(通过ViewStateUserKey或EnableViewStateMac),否则您就不走运了。
翻译自: https://www.hanselman.com/blog/http-posts-and-http-gets-with-webclient-and-c-and-faking-a-postback