WebProxy URI is not valid - c#

This is the code I currently have
using (WebClient client = new WebClient()) {
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(96.44.147.138:6060);
proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;
Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}
The proxy needs credentials.
I get an error on line proxy.Address = new Uri(96.44.147.138:6060);
saying
"The URI scheme is not valid."
Not sure what kind of value it's expecting

The Uri should consist of scheme host and optiona port. So you should use
proxy.Address = new Uri("http://96.44.147.138:6060");

Must be like;
using (var client = new WebClient())
{
var proxy = new WebProxy();
proxy.Address = new Uri("http://96.44.147.138:6060");
proxy.Credentials = new NetworkCredential(proxyUsername.Text, proxyPassword.Text);
proxy.UseDefaultCredentials = false;
proxy.BypassProxyOnLocal = false;
Console.WriteLine(client.DownloadString("http://bot.whatismyipaddress.com/"));
}
Example edit: Setting a global HTTP proxy in C# and .NET client classes

Related

.NET SOAP does not send BASIC auth in request header

Wanting to communicate with a SOAP webservice, I had C# classes created by SvcUtil.exe from the wsdl file.
When sending the Request below to a secure server (HTTPS with BASIC auth) I receive a System.ServiceModel.Security.MessageSecurityException and when checking the HTTP request by having traffic go though a Burp proxy I see that no BASIC auth information is passed. Is anything missing for the SOAP request in the C# code or what could be the problem that the BASIC auth does not work?
var binding = new WSHttpBinding();
binding.MessageEncoding = WSMessageEncoding.Mtom;
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Name = "BasicAuthSecured";
SearchServicePortClient searchClient = new SearchServicePortClient(binding, new EndpointAddress("https://myUrl:Port/myService"));
searchClient.ClientCredentials.UserName.UserName = "username";
searchClient.ClientCredentials.UserName.Password = "pw";
query soap = new query();
//...
queryResponse response = searchClient.query(soap);
Thanks in advance
This is another approach, don't know if it is the best though
using (var client = _clientFactory.GetClient())
{
var credentials = Utils.EncodeTo64("user123:password");
client.ChannelFactory.CreateChannel();
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
//operation
client.Send(request);
}
}
Try to use TransportWithMessageCredential:
binding.Security.Mode = SecurityMode.TransportWithMessageCredential;

The remote server returned an error: (401) Unauthorized Error occured when i try to download file via Web Client

I have the following code running in a windows form Application:
WebClient client = new WebClient();
WebProxy wp = new WebProxy("http://50.35.125.91:81/");
client.UseDefaultCredentials = true;
wp.Credentials = CredentialCache.DefaultCredentials;
wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");
client.Proxy = wp;
client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
Each time, I get the following exception
The remote server returned an error: (401) Unauthorized.
I know for sure the credentials are valid,
Provide credentials to proxy and client:
using (WebClient client = new WebClient())
{
WebProxy wp = new WebProxy("http://50.35.125.91:81/");
wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");
client.UseDefaultCredentials = false;
client.Credentials = wp.Credentials;
client.Proxy = wp;
client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
}
You are using the default credentials. The UseDefaultCredentials should be set to false instead of true:
client.UseDefaultCredentials = false;
Also this line you don't need at all:
wp.Credentials = CredentialCache.DefaultCredentials;
Try this:
using (var Client = new WebClient())
{
Client.Credentials = new NetworkCredential("matif", "yyy", "xyz");
Client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", #"D:\abc.xml");
}

get html source through proxy

Hi how do I get the source of an html page through a proxy. When I use the code below I get an error saying "Proxy Authentication Required." and I have to go through a proxy.
Dim client As New WebClient()
Dim htmlCode As String = client.DownloadString("http://www.stackoverflow.com")
Then use a proxy that does not need authentication
see here for more info
http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy.aspx
string source = GetPageSource("http://www.stackoverflow.com");
private string GetPageSource(string url)
{
string htmlSource = string.Empty;
try
{
System.Net.WebProxy myProxy = new System.Net.WebProxy("Proxy IP", 8080);
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Proxy = myProxy;
client.Proxy.Credentials = new System.Net.NetworkCredential("username", "password");
htmlSource = client.DownloadString(url);
}
}
catch (WebException ex)
{
// log any exceptions
}
return htmlSource;
}

how to add proxy authentication details to webclient object

I have some code that's throwing a 407 unauthorized exception.
I am trying to download a file and below is my example code. I've tried with netcredentials and webproxy but in vain.
WebClient webClient = new WebClient();
NetworkCredential netCred=new NetworkCredential();
netCred.UserName="<<userid>>";
netCred.Password="<<password>>";
netCred.Domain="<<windowsdomainname>>";
webClient.Credentials = netCred;
WebProxy wp = new WebProxy();
wp.Credentials = netCred;
wp.Address = new Uri(#"http://proxy-xx.xxxx.co.uk:8080/proxy.pac");
webClient.Proxy = wp;
webClient.DownloadFile("http://www.win-rar.com/postdownload.html?&L=0", #"c:\winrar.exe");
I was getting 407 before finding your question. Modified your source to the following, which works for me:
try
{
var netCred = new NetworkCredential { UserName = "ununun", Password = #"pwpwpw", Domain = #"domain" };
var webProxy = new WebProxy { Credentials = netCred };
var webClient = new WebClient { Proxy = webProxy };
webClient.DownloadFile(url, saveFileName);
}
catch (Exception ex)
{
Console.WriteLine("Exception:\n{0}\n{1}", ex.Message, ex.StackTrace);
return;
}
This is what I do yesterday.
WebClient client = new WebClient();
ICredentials cred;
cred = new NetworkCredential(user, password);
client.Proxy = new WebProxy("xxxx-proxy", true, null, cred);
Hope it help !

How to by pass network using WebProxy?

If I want to bypass a Network like 192.168.1.0/24 using webProxy is there any way?
WebProxy proxy = new WebProxy();
proxy.ByPassList = ???
You could set it up in Internet Explorer and then use
WebProxy proxy = (WebProxy) WebProxy.GetDefaultProxy(); Deprecated.
var iproxy = WebRequest.GetSystemWebProxy();
var url = new Uri("http://www.example.com");
var wp = new WebProxy();
wp.Credentials = iproxy.Credentials;
wp.Address = iproxy.GetProxy(url);
or you could try to add "192.\.168\.1\.*" to proxy.BypassList with something like
List<string> bypasslist = new List<string>(proxy.BypassList);
bypasslist.Add("192.\.168\.1\.*");
proxy.BypassList = bypasslist.ToArray();
You cannot alter the bypass list after the proxy creation. Use the following constructor overloads:
Uri address = ...
proxy = new WebProxy(address, **true**);
true means "bypass on local", and should be enough for you needs if you are using a 192.168.1.0/24 subnet.
or if you want to add a custom list:
Uri address = ...
proxy = new WebProxy(address, true, new string[] {"192.168.1.1","intranet",...});
Use this
WebProxy lb_proxy = new WebProxy("PROXY_IP", PROXY_PORT)
{
BypassProxyOnLocal = true,
BypassList = new string[] { "192.168.1.1", "192.168.1.2",...}
};
httpRequest.Proxy = lb_proxy;

Categories