Ajax跨域访问代理类,支持GET和POST方法

支持GET和POST两种方式
使用方法:http://localhost:4817/AppContainer/HttpProxy.ashx?httpproxy_request_url=http://www.g.cn&httpproxy_chars_set=gbk

ContractedBlock.gif ExpandedBlockStart.gif HttpProxy
 1<%@ WebHandler Language="C#" Class="HttpProxy" %>
 2
 3using System;
 4using System.Web;
 5using System.Text;
 6
 7public class HttpProxy : IHttpHandler
 8ExpandedBlockStart.gifContractedBlock.gif{
 9
10    public void ProcessRequest(HttpContext context)
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        HttpRequest Request = context.Request;
13        HttpResponse Response = context.Response;
14        try
15ExpandedSubBlockStart.gifContractedSubBlock.gif        {
16            string method = Request.HttpMethod;
17            System.Collections.Specialized.NameValueCollection data = new System.Collections.Specialized.NameValueCollection();
18            if (method == "POST")
19ExpandedSubBlockStart.gifContractedSubBlock.gif            {
20                data.Add(Request.Form);
21            }

22            else
23ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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))
29ExpandedSubBlockStart.gifContractedSubBlock.gif            {
30                chars_set = "gbk";
31            }

32            Response.ContentEncoding = System.Text.Encoding.GetEncoding(chars_set);
33            if (string.IsNullOrEmpty(request_url))
34ExpandedSubBlockStart.gifContractedSubBlock.gif            {
35                Response.Write("请求地址不能为空.");
36                Response.End();
37            }

38            else
39ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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")
45ExpandedSubBlockStart.gifContractedSubBlock.gif                {
46                    result = HttpHelper.Post(request_url, postdata, chars_set);
47                }

48                else
49ExpandedSubBlockStart.gifContractedSubBlock.gif                {
50
51                    result = HttpHelper.Get(request_url, chars_set);
52                }

53                Response.Write(result);
54                Response.End();
55            }

56        }

57        catch (Exception ex)
58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
59            Response.Write("请求发生错误:" + ex.ToString());
60            Response.End();
61        }

62    }

63
64    public bool IsReusable
65ExpandedSubBlockStart.gifContractedSubBlock.gif    {
66        get
67ExpandedSubBlockStart.gifContractedSubBlock.gif        {
68            return false;
69        }

70    }

71
72}

 


ContractedBlock.gif ExpandedBlockStart.gif HttpHelper
 1using System.Text;
 2using System.Net;
 3using System.IO;
 4using System.Collections.Specialized;
 5
 6
 7public class HttpHelper
 8ExpandedBlockStart.gifContractedBlock.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <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)
17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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())
25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
26            newStream.Write(postdata, 0, postdata.Length);
27        }

28        using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
30            using (Stream stream = response.GetResponseStream())
31ExpandedSubBlockStart.gifContractedSubBlock.gif            {
32                using (StreamReader reader = new StreamReader(stream, encoding, true))
33ExpandedSubBlockStart.gifContractedSubBlock.gif                {
34                    return reader.ReadToEnd();
35                }

36            }

37        }

38    }

39
40ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <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)
47ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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())
53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
54            myresponse.Cookies = cookie.GetCookies(myRequest.RequestUri);
55            using (Stream mystream = myresponse.GetResponseStream())
56ExpandedSubBlockStart.gifContractedSubBlock.gif            {
57                using (StreamReader myreader = new StreamReader(mystream, System.Text.Encoding.GetEncoding(chars_set), true))
58ExpandedSubBlockStart.gifContractedSubBlock.gif                {
59                    return myreader.ReadToEnd();
60                }

61            }

62        }

63    }

64ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <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)
71ExpandedSubBlockStart.gifContractedSubBlock.gif    {
72        StringBuilder sb = new StringBuilder();
73        for (int i = 0; i < data.Count; i++)
74ExpandedSubBlockStart.gifContractedSubBlock.gif        {
75            if (i != 0) sb.Append("&");
76            sb.Append(data.GetKey(i)).Append("=").Append(data[i]);
77        }

78        return sb.ToString();
79    }

80}

81

转载于:https://www.cnblogs.com/mobile/archive/2009/09/03/1559551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值