I'm trying to get a HTML source of a website through C# code. When I access the site with Windows Authentication, the following code works:
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
When I enter my domain credentials manually, I get an "unauthenticated" message.
using (WebClient client = new WebClient())
{
NetworkCredential credentials = new NetworkCredential("username", "pass", "domain");
client.Credentials = credentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
Why is it so?
Try this:
CredentialCache cc = new CredentialCache();
cc.Add(
new Uri("http://intranet/"),
"NTLM",
new NetworkCredential("username", "pass", "domain"));
client.Credentials = cc;
Related
Why can't I call web service method like that:
ws test = new ws();
test.Credentials = new NetworkCredential("UserName", "Password");
var temp = test.Method(1, "test");`
But this work:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
req.Credentials = new NetworkCredential("UserName", "Password");
using (WebResponse response = req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string result= reader.ReadToEnd();
}
}
Web service use https.
I get this error:
System.Net.WebException: The request failed with HTTP status 403: Forbidden.
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");
}
Trying to GetResponse From a web site;
using System.Text;
using System.Net;
using System.IO;
namespace DutyPharmacy751013
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding encoding = Encoding.GetEncoding(response.CharacterSet);
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, encoding);
string responseText= reader.ReadToEnd();
}
}
}
This code is working on win7 and LAN
and on win8 and any of wireless connection
but doesn't work on win8 and LAN error: 407 Proxy authentication required.
Is there any solution.
Thanks.
try with adding proxy credentials to request and also give network credentials
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
request.Credentials = new NetworkCredential("username", "pw");
WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
{
Credentials = new NetworkCredential("username", "pw"),
UseDefaultCredentials = false
};
request.Proxy = webProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//rest of the code...
Edit
For requests that you create, you can disable automatic proxy detection at the request level by using a null Proxy with your request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
request.Proxy = null;
//rest of the code
WebProxy webProxy = new WebProxy("http://myproxy.net:8080/", true)
{
UseDefaultCredentials = false,
Credentials = new NetworkCredential("username", "pw")
};
Please note Correct sequence to set property {other wise failed for me}
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 !
The following code keeps returning a 401:
String URI = "https://api.opsourcecloud.net/oec/0.9/myaccount";
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("username", "password");
string s = webClient.DownloadString(URI);
StreamReader reader = new StreamReader( webClient.OpenRead(URI));
On the last line. The password and url are correct. Any idea what I am doing wrong?
Have you tried adding the proxy and a domain?
WebClient client = new WebClient();
client.Proxy = new WebProxy("<<your proxy here>>");
client.Proxy.Credentials = new NetworkCredential("login", "password", "domain");