2010-06-28 69 views
0

我有以下代码绕过本地计算机的代理服务器,然后发送WebRequest。WebRequest从另一个IP生成而不是我的系统IP?

   System.Net.HttpWebRequest Request; 
       System.Net.WebResponse Response; 
       System.Net.CredentialCache MyCredentialCache; 

编辑1

//System.Net.WebProxy proxyObject = new WebProxy("http://172.24.1.87:8080",true); 

      string strRootURI = "http://172.24.18.240/webdav/"; 
      string strUserName = "UsName"; 
      string strPassword = "Pwd"; 
      // string strDomain = "Domain"; 
      string strQuery = ""; 
      byte[] bytes = null; 
      System.IO.Stream RequestStream = null; 
      System.IO.Stream ResponseStream = null; 
      System.Xml.XmlTextReader XmlReader = null; 

      try 
      { 
       // Build the SQL query. 
       strQuery = "myWebDavVerb"; 

       // Create a new CredentialCache object and fill it with the network 
       // credentials required to access the server. 
       MyCredentialCache = new System.Net.CredentialCache(); 
       MyCredentialCache.Add(new System.Uri(strRootURI), "Basic", new System.Net.NetworkCredential(strUserName, strPassword));//, strDomain) 


       // Create the HttpWebRequest object. 
       Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI); 


       // Add the network credentials to the request. 
       Request.Credentials = MyCredentialCache; 

         // Request.Proxy = proxyObject; 
        // Specify the method. 
        Request.Method = "PROPFIND"; 
    } 

现在,当我尝试执行我得到403错误。所以我检查了服务器日志,发现HTTP/1.0请求来自IP 172.24.1.87,而我的IP是172.24.17.220

有没有办法避免这种情况?我认为这是403错误的根本原因。

请帮忙。 谢谢,

Subhen

回答

2

该IP地址是你的代理服务器的地址...和你设置代理服务器的网络请求是代理。

你为什么期望它不使用代理?

注意,你绕过请求本地机器,从本地机器不,如果这是你的困惑点。

编辑:如果你真的想知道发生了什么,抓住Wireshark这将让你看到所有来自你的机器的数据包。

如果要指定“不使用代理”,那么这样做:

request.Proxy = GlobalProxySelection.GetEmptyWebProxy(); 
+0

我已删除了代理SETT默认代理的代理从我的代码现在,但现在的请求来自172.24.1.86 – Simsons 2010-06-28 11:56:31

+0

@Subhen:这可能是你的网络的默认代理? – 2010-06-28 12:05:28

+0

是的,我们可以避免这种情况。因为我尝试过使用Netdrive并通过NetMon进行监控,在这种情况下,NetDrive应用程序未使用代理服务器。它直接从我的IP生成请求。 – Simsons 2010-06-28 12:08:43

1

的HttpWebRequest对于其代理物业 默认值,这是始终WebRequest.GetSystemWebProxy的结果()这是你在IE浏览器中配置的代理

,如果你不想使用你需要重写

Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI); 
Request.Proxy = null; 
+0

@ Marc, 获取错误'System.Net.WebRequest'不包含'GetEmptyWebProxy'的定义 – Simsons 2010-06-28 12:21:12

+0

@subhen sry我的坏,我想念阅读文档,旧的方法是GlobalProxySelection.GetEmptyWebProxy()。在新版本中,您只需将代理设置为空 – 2010-06-28 12:25:28