使用WebClient和C#的HTTP POST和HTTP GET以及伪造回发

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&param2=value

GET /whatever/page.aspx?param1=value&param2=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&param2=value

POST /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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值