HttpWebResponse and Proxy w/ NetworkCredentials Not Working - c#

I am working on a project that uses proxy to obtain websites' HTML code.
Now, the trouble I have is that I want to use Username-Password authentication and not IP-Auth when connecting to the proxy.
I have written a sample code and ran it with Snippy. It worked. Then I copied the same code into a Visual Studio .NET 4.5 Project and it failed with the error: An existing connection was forcibly closed by the remote host when trying to get the response.
Here is the code:
WebProxy[] proxies = { new WebProxy("ip", port) };
proxies[0].Credentials = new NetworkCredential { UserName = "username", Password = "password" };
string url = "https://www.google.com/search?q=s&num=50";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Proxy = proxies[0];
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader response_stream = new StreamReader(res.GetResponseStream())
{
string html = response_stream.ReadToEnd();
Console.WriteLine(html);
}
}
I have tried different variations. When I switch to authorization by IP, add mine and comment out the NetworkCredentials assignment, the code works perfectly both in Snippy and Visual Studio.
But why does it fail when using NetworkCredentials?

OK, I found the culprit. It was my proxy provider.
Note for everybody ever running into problems when testing proxy connection with HttpWebRequest -- try another seller. It might save you a couple of minutes. Or hours.

Related

c# API request for WhosOff not working

I am trying to connect to an API, via C# and WinForms, to download some data from a server. I am using the latest version of visual studio (2017 at the time of writing).
The API I am using: https://www.whosoff.com/features/api/
As per the API setup, I have already got an authentication key and my IP has been white listed.
What I have so far:
try{
var request =(HttpWebRequest)WebRequest.Create("https://wr1.whosoff.com/api/whosoff?start_date=01-Apr-2018&end_date=25-Apr-2018");
request.Method = "GET";
request.Headers.Add("AUTH-KEY", "MY_AUTH_KEY");
var response = (HttpWebResponse)request.GetResponse();
string content = string.Empty;
using (var stream = response.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
}
}
}
} catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
}
This does not work - it throws an exception; "The underlying connection was closed: An unexpected error occurred on a send."
I can connect to other sites API's using this format - can anyone point me in the right direction?
UPDATE
So it turns out the issue was with TLS certificates and the fact that the API services requires TLS 1.2, which by default is turned off. It can be enabled by inserting the following code into your project
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
After inserting this code, everything worked as expected.
ORIGINAL POST
For anyone that is interested; the code above DID in fact work. It turns out that debug mode within Visual Studio causes this weird behavior - how and why I am not sure.
But when running the EXE file directly from the debug folder; it worked fine.
Weird. Anyway I ended up changing to Newtonsoft.JSON and using the following code;
var client = new RestSharp.RestClient("API URL");
/* Create a new request to send to the client */
var request = new RestSharp.RestRequest(RestSharp.Method.GET);
/* Add the correct headers for authentication and format */
request.AddHeader("AUTH-KEY", "MY KEY");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
/* Get the response */
var response = client.Execute(request);
var content = response.Content;

JSON - No connection could be made because the target machine actively refused it

I’m trying to debug a problem.
I have been given a url to which I can send JSON commands via HTTP POST. If I stick the URL into Chrome I get a properly formatted response – all good.
If I put the same url into internet explorer (IE 11) I get a ‘The website is unable to display the webpage -This error (HTTP 501 Not Implemented or HTTP 505 Version Not Supported) means that the website you are visiting doesn’t currently have the ability to display the webpage, or support the HTTP version used to request the page.’ – Not sure whats going here
I ultimately need to send a command via a web service, to this end I have created a very simple app in visual studio.
I use this code, but then I get the error:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://URL.....");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
operation = "****",
username = "****",
password = "****"
});
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
{"No connection could be made because the target machine actively refused it 1.1.1.1:443"}
There is a problem, but being new to this I have no idea where to start looking.
Any pointers appreciated.

WebClient.DownloadData(uri) HTTP NTLM Authentication fails with 401 using correct credentials

I have the following code:
using (WebClient wcli = new WebClient())
{
wcli.UseDefaultCredentials = true;
wcli.Credentials = new NetworkCredential("RS_Username", "RS_Password", "RS_Domain");
byte[] buff = wcli.DownloadData(www);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + ".pdf\"");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.BinaryWrite(buff);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
I use it get the result of a report in SSRS 2014 and have it as a document download from my web application (in .Net 3.5, hosted on Window 8.1, IIS 8.5).
The problem I have is that I keep getting 401 Unauthorized when calling wcli.DownloadData(www) (Note the using any browser the reports are working fine with the credentials used)
I have done a TCP Dump and I have found out that the NTLM handshake is not occurring:
C -> S: GET Request
C <- S: '401 Unauthorized' response with header
'WWW-Authenticate: NTLM'
Nothing else
Another application hosted on the same machine but using .Net 4.5 uses the same code without any problem.
I believe it has to be due to a missing/wrong configuration, but I do not succeed to figure out which one.
Any ideas?
UPDATE
What I have forgotten to mention is that both web applications mentioned (both hosted on the same server and IIS) are connecting to the same Reporting Services server (but different folders).
I have had the same issue, in my case (Still .Net4.5) what worked was to use:
WebClient wc = new WebClient();
wc.UseDefaultCredentials = false;
string host = "http://localhost"; //this comes from a function in my code
var myCredentialCache = new System.Net.CredentialCache();
myCredentialCache.Add(new Uri(host + "/"), "NTLM", new System.Net.NetworkCredential(accessUser, accessPassword, domain));
wc.Credentials = myCredentialCache;
var result = wc.DownloadFile(www);
The main diference for me was to use a CredentialCache and setting the host uri, also the domain in the network credential
You're telling the WebClient to use the DefaultCredentials. wcli.UseDefaultCredentials = true; but you're also passing in a NetworkCredential. Perhaps the other code that works, if they're both using the same code, is working because it's user has access.
using (WebClient wcli = new WebClient())
{
wcli.UseDefaultCredentials = true;
wcli.Credentials = new NetworkCredential("RS_Username", "RS_Password", "RS_Domain");
}
However, that may not be your bug. Came across this article: http://www.benjaminathawes.com/2010/10/14/ntlms-dependency-on-http-keep-alives-another-cause-of-the-dreaded-401-1-error/.
It mentions the need to use HTTP keep-alive to keep the TCP connection open for the NTLM handshake. The fact that the handshake just dies leads me to think that maybe that could also be the issue. I would check to verify that keep-alive is indeed on.

C# WebRequest returning 401

There is a web file within my intranet that my computer is authorized to read and write. I can open up IE or Firefox and view the file by typing int the url address. I need to write a C# desktop app that reads/writes to that file. Even though my computer has access, all my attempts so far result in 401, unauthorized access errors. The program needs to work from any computer whose account has been authorized, so I cannot hard-code any username/password. I've never done anything like this, but I was able to scrounge the following from several sites:
WebRequest objRequest = HttpWebRequest.Create("https://site.com/file");
objRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
objRequest.Proxy = WebRequest.DefaultWebProxy;
objRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse objResponse = (WebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
string str = sr.ReadToEnd();
sr.Close();
//... Do stuff with str
}
If it matters, I'm working in .NET 2.0
Just ran into the same problem, it all started working when I added:
objRequest.UseDefaultCredentials = true;
Did you try using Fiddler to inspect the actual request that was sent to the server?
You can also check if the server requires a client certificate to allow the connection.
Since you are accessing an intranet server, do you really need to set the proxy part? I mean most of the time, the proxy is configured to ignore local addresses anyway.
This won't work if NTLM credentials are required:
objRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
You need to pass in the actual credentials like:
NetworkCredential networkCredential = new NetworkCredential(UserName, Password, Domain);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(url), "NTLM", networkCredential);
objRequest.Proxy.Credentials = credCache;

Getting 401 error when trying to read file from sharepoint repository

I have been using this code to read in a file from a document repository in sharepoint:
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create("http://servername/realestate/SiteAssets/navigation.txt");
objRequest.Timeout = 10000;
objRequest.UseDefaultCredentials = true;
objRequest.Proxy.Credentials = objRequest.Credentials;
objResponse = (WebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
navBar = sr.ReadToEnd();
sr.Close();
}
I just migrated to a new environment, and I am getting a 401 unauthorized error with this code - but, instead of using servername, I am now using hostname (since the new environment has a domain assigned to it). How can I navigate this issue, even though I am now using a host in my HttpWebRequest object? And, if this should not be causing the issue, I would like to hear recommendations as well. Thanks.
Local Loopback check?
KB 896861 - You receive error 401.1 when you browse a Web site that uses Integrated Authentication and is hosted on IIS 5.1 or a later version

Categories

Resources