views:

50

answers:

2

Hi, I have the following code which bypass the proxy server for local machine and then send a WebRequest.

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

Edit 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";
    }

Now, while I tried executing I got 403 error. So I checked in the server log and find that the HTTP/1.0 request is coming form an IP 172.24.1.87 while my IP is 172.24.17.220.

Is there a way to avoid this? I think this is the root cause of 403 error.

Please help. Thanks,

Subhen

+2  A: 

That IP address is the address of your proxy... and you're setting the proxy for the web request to be that proxy.

Why would you expect it not to use the proxy?

Note that you're bypassing requests to the local machine, not from the local machine, if that was your point of confusion.

EDIT: If you really want to know what's going on, get hold of Wireshark which will let you see all the packets coming from your machine.

If you want to specify "don't use a proxy" then do something like:

request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
Jon Skeet
I have removed the Proxy settings from my Code now , but now the request is coming from 172.24.1.86
Subhen
@Subhen: Is that perhaps the default proxy for your network?
Jon Skeet
Yes, Is there a way we can avoid this. Because I tried using Netdrive and monitored it through NetMon , in this case the NetDrive application is not using the Proxy Server. It is directly generating the request directly from my IP.
Subhen
@subhen: You can certainly avoid using the proxy using something like `GlobalProxySelection.GetEmptyWebProxy()`.
Jon Skeet
+1  A: 

HttpWebRequest has a default value for its Proxy Property this is always the result of WebRequest.GetSystemWebProxy() which is the Proxy you configured in IE

if you dont want to use a proxy you need to override the default proxy

Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI); 
Request.Proxy = null;
marc.d
@ Marc, Getting the error 'System.Net.WebRequest' does not contain a definition for 'GetEmptyWebProxy'
Subhen
@subhen sry my bad, i miss read the docs, the old method was GlobalProxySelection.GetEmptyWebProxy(). in the new version you just have to set proxy to null
marc.d