I'm making simple HTTP GET request to website using WebRequest. When I add proxy details,getting HTTP 405 method not allowed error.
Below is my code:
WebRequest req = HttpWebRequest.Create(uri);
//WebProxy prr = new WebProxy();
WebProxy proxy = new WebProxy("xxxxx");
req.Credentials = CredentialCache.DefaultCredentials;
req.Method = "GET";
req.Proxy = proxy;
HttpWebResponse resp=req.GetResponse();
"xxxx" is our org proxy URL.
It's working fine WITHOUT proxy, but I need to make it to work with proxy details.
Am I missing anything?.
Along with the proxy you may need to mention the port also. See the code below
string ipAddrs= "proxy ip ";
WebProxy proxy = new WebProxy(ipAddrs,3128);
default proxy port number is 3128
Related
I have a piece of old code (a single .aspx file) that I need to get through a proxy. This code used to work, but now the company have tightened up on security.
The offending line of code is:
dataSet.ReadXml(url);
The url is https.
It is running on .NET version 2.0 - this cannot be upgraded.
I cannot change the web.config file.
What do I need to add to the .aspx file to get it to work?
The error I am getting is:
The remote server returned an error: (407) Proxy Authentication Required.
There is no "connecting to the web" code in the script.
EDIT
Based on Dan's comment, I have tried this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
IWebProxy proxy = request.Proxy;
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://10.79.30.190:8080");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("username", "password");
request.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(responseString);
but am still getting the same error
EDIT
Another attempt:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
IWebProxy proxy = request.Proxy;
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://10.79.30.190:8080");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("username", "password");
request.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(new StreamReader(response.GetResponseStream()));
but am still getting the same error
it means, that your credentials for the proxy server are incorrect, best solution to try and approach this problem would be:
First, add this line to your Web.Config:
<system.net>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>
Second, is through code:
service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
third, is to set the credentials in two locations through code:
HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");
it's whatever suits you best here.
How can I send an HTTP request with Negotiate Authorization header attribute from a .NET (C#) application?
I tried the following, but Authorization attribute was not added to the request...
...
string url = ...;
WebRequest request = WebRequest.Create(url);
request.Credentials = GetCredential();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
...
private CredentialCache GetCredential()
{
string url = ...;
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new System.Uri(url), "Ntlm", new NetworkCredential(username, pwd, domain));
return credentialCache;
}
My experience with using a network credential in a WebRequest is that the request.GetResponse() does NOT pass the credential unless it receives an Unauthorized (challenge) response from the server. If it does receive a 403, it will automatically fire a second request which includes the credential. Make sure the end point you are hitting returns a 401 if the Auth header is missing.
I have a web role in which I have the following code snippet:
var webRequest = HttpWebRequest.Create(path);
webRequest.Timeout = 5000;
webRequest.Method = "GET";
It's requesting a resource from Azure CDN, so path is something like the following:
https://<uniqueString>.vo.msecnd.net/<container>/path/image.png
I noticed that the response I am getting is cached, because I checked in Fiddler, going directly to the resource, that I have different headers from the following webResponse.
var webResponse = (HttpWebResponse)webRequest.GetResponse();
So HttpRequest enforces some kind of CachePolicy here?
I tried replacing the first snippet with the following code but it didn't work:
WebRequest.DefaultCachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
var webRequest = HttpWebRequest.Create(path);
webRequest.Timeout = 5000;
webRequest.Method = "GET";
Only when I did set the headers it did work, so the following code gives me the correct response:
var webRequest = HttpWebRequest.Create(path);
webRequest.Timeout = 5000;
webRequest.Method = "GET";
webRequest.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0, no-cache, no-store");
Why HttpWebRequest was enforcing cache in the response and why the DefaultCachePolicy did not work after I tried to set it, except only explicitly setting the headers worked?
Note: The response was only caching when requesting the resource over HTTPS
Update
To be sure I've just added a query string parameter to the requested resource URI, putting the time ticks. Is this enough for me to not have cached responses?
I have a REST-based web service I'm trying to connect to, and while it works just fine from my local dev machine, I am trouble getting it to work on my client's test system which is behind a web proxy.
I have this code snippet here:
HttpWebRequest request = WebRequest.Create(targetUrl) as HttpWebRequest;
if (request == null)
return;
request.Method = "GET";
request.Accept = "application/json";
request.Credentials = new NetworkCredential("myusername", "mytopsecretpassword");
WebProxy webProxy = new WebProxy("myproxy.net", 8080)
{
Credentials = new NetworkCredential("myusername", "mytopsecretpassword"),
UseDefaultCredentials = false
};
request.Proxy = webProxy;
But when I try to execute this call and get back the response like this:
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
I keep getting an error:
System.Net.WebException
The remote server returned an error: (407) Proxy Authentication Required.
WTF ?!?! I'm setting up my proxy and I'm providing the proxy credentials.... what more can I do?
Try adding the domain to the proxy credentials.
I'd also try setting UseDefaultCredentials = false before you set the new credentials.
I'd like to capture the HTML code off a page and place it into a text file. Unfortunatel I get an error of the following:
"The remote server returned an error: (407) Proxy Authentication Required."
Anyone know how to solve this?
string url = #"http://www.panalpina.com/www/global/en/tools_resources/unit_converter/currency_codes.html";
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";
// make request for web page
myWebRequest.ToString();
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
string myPageSource = string.Empty;
myPageSource = myWebSource.ReadToEnd();
myWebResponse.Close();
`
It seems you are using proxy server to connect to internet.
If yes add next code before http request send:
myWebRequest.Proxy = new WebProxy("you_proxy_machine", 8080 /*port*/);
myWebRequest.Proxy.Credentials = new NetworkCredential("proxy_username", proxy_password");