HttpWebRequest WebResponse PROXY URLENCODE 2 代码 HttpWebRequest WebResponse PROXY URLENCODE 1 http://www.shengfang.org/blog/p/HttpWebReqRespPROXYURLENC1.php 模拟提交: private void baidu_Click(object sender, System.EventArgs e) { getPage("http://www.shengfang.org/blog/index.php", "job=showsearch&keywords=mysql"); } public static void getPage(String url, String payload) { WebResponse result = null; www.shengfang.org try { www.shengfang.org HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; www.shengfang.org req.ContentType = "application/x-www-form-urlencoded"; req.Accept ="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; StringBuilder UrlEncoded = new StringBuilder(); Char[] reserved = { '?', '=', '&' }; byte[] SomeBytes = null; if (payload != null) { int i=0, j; while(i<payload.Length) { j=payload.IndexOfAny(reserved, i); if (j==-1) www.shengfang.org { UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i))); break; } UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i))); UrlEncoded.Append(payload.Substring(j,1)); i = j+1; } Trace.WriteLine(UrlEncoded.ToString()); SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString()); SomeBytes = Encoding.Default.GetBytes( "stext=%CA%A2%B7%C5&imageField.x=22&imageField.y=7"); //SomeBytes = Encoding.UTF8.GetBytes("s?wd=%CA%A2%B7%C5&cl=3"); req.ContentLength = SomeBytes.Length; Stream newStream = req.GetRequestStream(); newStream.Write(SomeBytes, 0, SomeBytes.Length); newStream.Close(); } else www.shengfang.org { req.ContentLength = 0; } www.shengfang.org result = req.GetResponse(); Stream ReceiveStream = result.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader sr = new StreamReader( ReceiveStream, System.Text.Encoding.Default ); Trace.WriteLine("\r\n已接收到响应流"); Char[] read = new Char[256]; int count = sr.Read( read, 0, 256 ); Trace.WriteLine("HTML...\r\n"); while (count > 0) { String str = new String(read, 0, count); Trace.Write(str); count = sr.Read(read, 0, 256); } Trace.WriteLine(""); } catch(Exception e) www.shengfang.org { Trace.WriteLine( e.ToString()); Trace.WriteLine("\r\n找不到请求 URI,或者它的格式不正确"); } finally { if ( result != null ) { result.Close(); } } } www.shengfang.org 字体:大 中 小 |