支持GET和POST两种方式 使用方法:http://localhost:4817/AppContainer/HttpProxy.ashx?httpproxy_request_url=http://www.g.cn&httpproxy_chars_set=gbk
1<%@ WebHandler Language="C#" Class="HttpProxy" %>
2
3using System;
4using System.Web;
5using System.Text;
6
7public class HttpProxy : IHttpHandler
8{
9
10 public void ProcessRequest(HttpContext context)
11 {
12 HttpRequest Request = context.Request;
13 HttpResponse Response = context.Response;
14 try
15 {
16 string method = Request.HttpMethod;
17 System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
18 if (method == "POST")
19 {
20 data.Add(Request.Form);
21 }
22 else
23 {
24 data.Add(Request.QueryString);
25 }
26 string request_url = data["httpproxy_request_url"];
27 string chars_set = data["httpproxy_chars_set"];
28 if (string.IsNullOrEmpty(chars_set))
29 {
30 chars_set = "gbk";
31 }
32 Response.ContentEncoding = System.Text.Encoding.GetEncoding(chars_set);
33 if (string.IsNullOrEmpty(request_url))
34 {
35 Response.Write("请求地址不能为空.");
36 Response.End();
37 }
38 else
39 {
40 data.Remove("httpproxy_request_url");
41 data.Remove("httpproxy_chars_set");
42 string postdata = HttpHelper.ToNameValueString(data);
43 string result = string.Empty;
44 if (method == "POST")
45 {
46 result = HttpHelper.Post(request_url, postdata, chars_set);
47 }
48 else
49 {
50
51 result = HttpHelper.Get(request_url, chars_set);
52 }
53 Response.Write(result);
54 Response.End();
55 }
56 }
57 catch (Exception ex)
58 {
59 Response.Write("请求发生错误:" + ex.ToString());
60 Response.End();
61 }
62 }
63
64 public bool IsReusable
65 {
66 get
67 {
68 return false;
69 }
70 }
71
72}
1using System.Text;
2using System.Net;
3using System.IO;
4using System.Collections.Specialized;
5
6
7public class HttpHelper
8{
9 /**//// <summary>
10 /// 向指定地址发送POST请求
11 /// </summary>
12 /// <param name="getUrl">指定的网页地址</param>
13 /// <param name="postData">POST的数据(格式为:p1=v1&p1=v2)</param>
14 /// <param name="chars_set">可采用如UTF-8,GB2312,GBK等</param>
15 /// <returns>页面返回内容</returns>
16 public static string Post(string postUrl, string postData, string chars_set)
17 {
18 Encoding encoding = Encoding.GetEncoding(chars_set);
19 HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(postUrl);
20 Request.Method = "POST";
21 Request.ContentType = "application/x-www-form-urlencoded";
22 Request.AllowAutoRedirect = true;
23 byte[] postdata = encoding.GetBytes(postData);
24 using (Stream newStream = Request.GetRequestStream())
25 {
26 newStream.Write(postdata, 0, postdata.Length);
27 }
28 using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
29 {
30 using (Stream stream = response.GetResponseStream())
31 {
32 using (StreamReader reader = new StreamReader(stream, encoding, true))
33 {
34 return reader.ReadToEnd();
35 }
36 }
37 }
38 }
39
40 /**//// <summary>
41 /// 想地址发送GET请求
42 /// </summary>
43 /// <param name="getUrl">地址(格式:http://host/page?p1=v1&p2=v2</param>
44 /// <param name="chars_set">可采用如UTF-8,GB2312,GBK等</param>
45 /// <returns>页面返回内容</returns>
46 public static string Get(string getUrl, string chars_set)
47 {
48 CookieContainer cookie = new CookieContainer();
49 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(getUrl);
50 myRequest.AllowAutoRedirect = true;
51 myRequest.CookieContainer = cookie;
52 using (HttpWebResponse myresponse = (HttpWebResponse)myRequest.GetResponse())
53 {
54 myresponse.Cookies = cookie.GetCookies(myRequest.RequestUri);
55 using (Stream mystream = myresponse.GetResponseStream())
56 {
57 using (StreamReader myreader = new StreamReader(mystream, System.Text.Encoding.GetEncoding(chars_set), true))
58 {
59 return myreader.ReadToEnd();
60 }
61 }
62 }
63 }
64 /**//// <summary>
65 /// 生成NameValueCollection字符串
66 /// 字符串格式如下:p1=v1&p2=v2&p3=v3&p4=v4
67 /// </summary>
68 /// <param name="data"></param>
69 /// <returns>字符串格式如下:p1=v1&p2=v2&p3=v3&p4=v4</returns>
70 public static string ToNameValueString(NameValueCollection data)
71 {
72 StringBuilder sb = new StringBuilder();
73 for (int i = 0; i < data.Count; i++)
74 {
75 if (i != 0) sb.Append("&");
76 sb.Append(data.GetKey(i)).Append("=").Append(data[i]);
77 }
78 return sb.ToString();
79 }
80}
81