I am beginner. Take it easy on me.
I want to set or connect proxy on that piece of code
var requestMsg = new HttpRequestMessage(GetHttpMethod(method), url);
if (method != APIMethod.GET)
{
var serializedContent = JsonConvert.SerializeObject(request);
requestMsg.Content = new StringContent(serializedContent, Encoding.UTF8, "application/json");
}
WebProxy myproxy = new WebProxy("xxx.xx.xx.xx", 8080);
requestMsg.Proxy = myproxy; <------ error
HttpResponseMessage task = await httpClientFactory.CreateClient().SendAsync(requestMsg);
please help me set proxy hard code
i read these but i cant understand
Proxy with HTTP Requests
C# Connecting Through Proxy
You can try to use HttpClient.DefaultProxy to set the global Http proxy.
HttpClient.DefaultProxy = new WebProxy("xxx.xx.xx.xx", 8080);
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.
I found several questions about this but they seem to resolve windows authentication. I have custom users and passwords and this code works with other existing proxies. Now since a week we use new proxy servers that use {Basic realm="Private Proxies - Password Auth"} and the code does not work.
var user = "********";
var pass = "********";
var port = "********";
var ip = "********";
var url = "********";
var baseUrl = "********";
NetworkCredential cr = new NetworkCredential(user, pass);
cr.Domain = ip;
WebProxy proxy = new WebProxy(ip + ":" + port) { BypassProxyOnLocal = false, Credentials = cr, UseDefaultCredentials = false};
HttpClientHandler handler = new HttpClientHandler();
handler.Proxy = proxy;
handler.UseProxy = true;
HttpClient httpClient = new HttpClient(handler);
httpClient.BaseAddress = new Uri(baseUrl);
var result = httpClient.GetAsync(url).Result;
Console.WriteLine(result.Content.ReadAsStringAsync().Result);
Console.Read();
I have tested the credentials with Firefox and there the proxies work after I provide the credentials in a popup. I set the NetworkCredential inside the proxy, but when I do a request it just returns 407: Proxy Authentication Required.
I tried to set the Proxy-Authorization header manually. It still does not work.
Does anyone know how to fix this!?
You need to fill Proxy-Authorization header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization
Also, look at this question: Proxy Basic Authentication in C#: HTTP 407 error
using (var client = new System.Net.Http.HttpClient())
{
var response = client.GetAsync(fullUrl).Result;
}
I am creating HTTP client as above to consume a RESTfull service.
I should be able to set proxy for this service request.
How can I set proxy server specific to this service request only?
System.Net.Http.HttpClient don't have TransportSettings propert and about Microsoft.Http assembly is unknown.
uSING Microsoft.Http I can
HttpClient client = new HttpClient();
/*Set Credentials to authenticate proxy*/
client.TransportSettings.Proxy = new WebProxy(proxyAddress);
client.TransportSettings.Proxy.Credentials = CredentialCache.DefaultCredentials;
client.TransportSettings.Credentials = CredentialCache.DefaultCredentials;
client.BaseAddress = new Uri(this.baseUrl);
var response = client.Get(fullUrl);
var jsonResponce = response.Content.ReadAsJsonDataContract<mYResponseoBJECT>();
public static T ReadAsJsonDataContract<T>(this HttpContent content)
{
return (T)content.ReadAsJsonDataContract<T>(new DataContractJsonSerializer(typeof(T)));
}
I am working with a proxy that requires authentication, i.e., in a browser if I try to open a page it will immediately ask for credentials. I supplied same credentials in my program but it fails with HTTP 407 error.
Here is my code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
IWebProxy proxy = WebRequest.GetSystemWebProxy();
CredentialCache cc = new CredentialCache();
NetworkCredential nc = new NetworkCredential();
nc.UserName = "userName";
nc.Password = "password";
nc.Domain = "mydomain";
cc.Add("http://20.154.23.100", 8888, "Basic", nc);
proxy.Credentials = cc;
//proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy = proxy;
request.Proxy.Credentials = cc;
request.Credentials = cc;
request.PreAuthenticate = true;
I have tried every possible thing but seem like I am missing something.
Is it something like, I have to make two requests? First with out credentials and once I hear back from server about need for credentials, make same request with credentials?
This method may avoid the need to hard code or configure proxy credentials, which may be desirable.
Put this in your application configuration file - probably app.config. Visual Studio will rename it to yourappname.exe.config on build, and it will end up next to your executable. If you don't have an application configuration file, just add one using Add New Item in Visual Studio.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
I was getting a very similar situation where the HttpWebRequest wasn't picking up the correct proxy details by default and setting the UseDefaultCredentials didn't work either. Forcing the settings in code however worked a treat:
IWebProxy proxy = myWebRequest.Proxy;
if (proxy != null) {
string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString();
myWebRequest.UseDefaultCredentials = true;
myWebRequest.Proxy = new WebProxy(proxyuri, false);
myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}
and because this uses the default credentials it should not ask the user for their details.
here is the correct way of using proxy along with creds..
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
IWebProxy proxy = request.Proxy;
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
}
else
{
Console.WriteLine("Proxy is null; no proxy will be used");
}
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://20.154.23.100:8888");
// 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;
Thanks everyone for help... :)
This problem had been bugging me for years the only workaround for me was to ask our networks team to make exceptions on our firewall so that certain URL requests didn't need to be authenticated on the proxy which is not ideal.
Recently I upgraded the project to .NET 4 from 3.5 and the code just started working using the default credentials for the proxy, no hardcoding of credentials etc.
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
I had a similar problem due to a password protected proxy server and couldn't find much in the way of information out there - hopefully this helps someone. I wanted to pick up the credentials as used by the customer's browser. However, the CredentialCache.DefaultCredentials and DefaultNetworkCredentials aren't working when the proxy has it's own username and password even though I had entered these details to ensure thatInternet explorer and Edge had access.
The solution for me in the end was to use a nuget package called "CredentialManagement.Standard" and the below code:
using WebClient webClient = new WebClient();
var request = WebRequest.Create("http://google.co.uk");
var proxy = request.Proxy.GetProxy(new Uri("http://google.co.uk"));
var cmgr = new CredentialManagement.Credential() { Target = proxy.Host };
if (cmgr.Load())
{
var credentials = new NetworkCredential(cmgr.Username, cmgr.Password);
webClient.Proxy.Credentials = credentials;
webClient.Credentials = credentials;
}
This grabs credentials from 'Credentials Manager' - which can be found via Windows - click Start then search for 'Credentials Manager'. Credentials for the proxy that were manually entered when prompted by the browser will be in the Windows Credentials section.
You can use like this, it works!
WebProxy proxy = new WebProxy
{
Address = new Uri(""),
Credentials = new NetworkCredential("", "")
};
HttpClientHandler httpClientHandler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true
};
HttpClient client = new HttpClient(httpClientHandler);
HttpResponseMessage response = await client.PostAsync("...");
try this
var YourURL = "http://yourUrl/";
HttpClientHandler handler = new HttpClientHandler()
{
Proxy = new WebProxy("http://127.0.0.1:8888"),
UseProxy = true,
};
Console.WriteLine(YourURL);
HttpClient client = new HttpClient(handler);