System.Data.DataSet.ReadXml - Proxy Authentication Required - c#

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.

Related

set IP and Port in proxy in c#

i want to send request to instagram with this address :"https://i.instagram.com"
and i want to use proxy for each request that i have send
which one is ok?
and the uri (""https://i.instagram.com:8080"") is ok?
if second code is ok then what is the NetworkCredential
IWebProxy Proxya = System.Net.WebRequest.GetSystemWebProxy();
//to get default proxy settings
Proxya.Credentials = CredentialCache.DefaultNetworkCredentials;
Uri targetserver = new Uri("https://i.instagram.com:8080");
Uri proxyserver = Proxya.GetProxy(targetserver);
HttpClientHandler handler = new HttpClientHandler();
handler.Proxy = Proxya;
second
IWebProxy Proxya = System.Net.WebRequest.GetSystemWebProxy();
//to get default proxy settings
Proxya.Credentials = new NetworkCredential("xxxx", "xxxx");
Uri targetserver = new Uri("https://i.instagram.com:8080");
Uri proxyserver = Proxya.GetProxy(targetserver);
HttpClientHandler handler = new HttpClientHandler();
handler.Proxy = Proxya;
You have to take care of what you want to do.
Right now, you're telling your program to take the locally defined proxy server via GetSystemWebProxy(). This means that the program uses the proxy defined in your system's proxy settings.
After that you're telling the program here:
Uri targetserver = new Uri("https://i.instagram.com:8080");
Uri proxyserver = Proxya.GetProxy(targetserver);
That your proxy server is listening on https://i.instagram.com:8080. This should be part of your WebRequest.
Now Proxya.Credentials = new NetworkCredential("xxxx", "xxxx"); simple says that your proxy server requires authentication via username and password.
Does your proxy server allow anonymous login? If yes, then you don't need it.
But(!) I wouldn't recommend providing an open proxy.
I'd suggest you split your code into two parts:
The Proxy code part:
Define your proxy settings here:
string proxyAddress = "proxyAddress";
int proxyPort = 1337;
string proxyUser = "user";
string proxyPassword = "password";
IWebProxy proxy = new WebProxy(proxyAddress, proxyPort)
{
Credentials = new NetworkCredential(proxyUser, proxyPassword)
};
Or if you don't use a proxy, simple don't define one.
If you're using the proxy defined in your system's settings, then this should suffice:
IWebProxy proxy = WebRequest.GetSystemWebProxy();
The WebRequest or HttpClientHandler itself.
string instagramAddress = "https://i.instagram.com:8080";
Uri targetserver = new Uri(instagramAddress);
// HttpClientHandler handler = new HttpClientHandler();
WebRequest request = WebRequest.Create(targetserver);
// handler.Proxy = proxy;
request.Proxy = proxy; //Set the previously defined proxy here

C# webrequest to curl

I need to make a GET request to following url
[ ~ ] $ curl -u duff:X https://subs.pinpayments.com/api/v4/sitename/subscribers/7388.xml
where -u is username and then X is password.
How to use WebRequest?
Please suggest
The WebRequest class has a Credentials property, which you can set:
WebRequest request = WebRequest.Create(uri);
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Another possibility would be to use the WebClient class, that supports custom credentials too:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
//Byte[] pageData = client.DownloadData(url);
//string pageHtml = Encoding.ASCII.GetString(pageHtml);
// or DownloadString: http://msdn.microsoft.com/en-us/library/fhd1f0sw%28v=vs.110%29.aspx
var pageHtml = client.DownloadString(uri);
Console.WriteLine(pageHtml);
If you need for a reason to set custom header information for the request, then the WebClient class could be more suitable.

HttpWebRequest doesn't seem to "get" my proxy credentials

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.

connect to website using a free proxy server programmatically

I need to connect to a website using a proxy server. I can do this manually, for example I can use the online proxy http://zend2.com and then surf to www.google.com. But this must be done programmatically. I know I can use WebProxy class but how can I write a code so a proxy server can be used?
Anyone can give me a code snippet as example or something?
thanks
Understanding of zend2 works, you can populate an url like this :
http://zend2.com/bro.php?u=http%3A%2F%2Fwww.google.com&b=12&f=norefer
for browsing google.
I C#, build the url like this :
string targetUrl = "http://www.google.com";
string proxyUrlFormat = "http://zend2.com/bro.php?u={0}&b=12&f=norefer";
string actualUrl = string.Format(proxyUrlFormat, HttpUtility.UrlEncode(targetUrl));
// Do something with the proxy-ed url
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(actualUrl));
HttpWebResponse resp = req.GetResponse();
string content = null;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
content = sr.ReadToEnd();
}
Console.WriteLine(content);
You can use WebProxy Class
MSDN code
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
In your case
WebProxy proxyObject = new WebProxy("http://zend2.com",true);
WebRequest req = WebRequest.Create("www.google.com");
req.Proxy = proxyObject;

Proxy Basic Authentication in C#: HTTP 407 error

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);

Categories