北京经济技术开发区经开区虚拟城市项目-【前端】-移动端Web
11
少年
2024-02-06 0261ec79dc29b9ff0e1068b060c7414234028e23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<%@ WebHandler Language="C#" Class="ZDproxy" Debug="true" %>
 
using System;
using System.Web;
using System.Net;
using System.IO;
using Microsoft.Win32;
 
public class ZDproxy : IHttpHandler
{
    
    public void ProcessRequest (HttpContext context) 
    {
        Stream outStream = null;
        HttpWebRequest myHttpWebRequest = null;
        HttpWebResponse myHttpWebResponse = null;
        try
        {
            //string url = Convert.ToString(context.Request.QueryString["url"]);      
            string Indicator="url=";
            string url = context.Request.Url.ToString();
            int index = url.IndexOf(Indicator);
            string urlpar = url.Substring(index + Indicator.Length, (url.Length - index - Indicator.Length));
 
            myHttpWebRequest = (HttpWebRequest)WebRequest.Create(urlpar);
            myHttpWebRequest.Method = context.Request.HttpMethod;
            WebProxy myProxy = new WebProxy();
            //object localMachProxy = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", "NOT FOUND");
            //if (localMachProxy != null && localMachProxy.ToString() != "NOT FOUND")
            //{
            //    Uri newUri = new Uri("http://" + localMachProxy.ToString());//设置代理服务器的地址
            //    myProxy.Address = newUri;
            //    object ProxyOverrid = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", "NOT FOUND");
            //    if (ProxyOverrid != null && ProxyOverrid.ToString() != "NOT FOUND")
            //    {
            //        myProxy.BypassList = ProxyOverrid.ToString().Split(';');
            //    }
            //    //myHttpWebRequest.Proxy = myProxy;
            //}
            //else
            //{
            //    Uri newUri = new Uri("http://127.0.0.1:1080");//设置代理服务器的地址
            //    myProxy.Address = newUri;
            //   // myHttpWebRequest.Proxy = myProxy;
            //}
            if (context.Request.HttpMethod == "POST")
            {
                myHttpWebRequest.ContentType = "text/plain; charset=utf-8";
                System.IO.Stream requestStream = context.Request.InputStream;
                byte[] requestByts=new byte[requestStream.Length];
                requestStream.Read(requestByts, 0, Convert.ToInt32(requestStream.Length));
                System.IO.Stream myRequestStream = myHttpWebRequest.GetRequestStream();
                myRequestStream.Write(requestByts, 0, Convert.ToInt32(requestStream.Length));
                myRequestStream.Close();
            }
            
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            outStream = myHttpWebResponse.GetResponseStream();
            StreamReader sr = new StreamReader(outStream);
            context.Response.ContentType = "text/xml";
            context.Response.Write(sr.ReadToEnd());
            
        }
        catch (Exception ex)
        {
            myHttpWebRequest.ContentType="text/xml";
            context.Response.Write("请求失败:"+ex.Message);
        }
        finally
        {
            if (outStream != null)
                outStream.Close();
            if (myHttpWebResponse != null)
                myHttpWebResponse.Close();
        }
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }
 
}