401 with webclient https - c#

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

Related

Cant call method from web service

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.

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

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 !

Webclient.UploadString() sends an empty string

Here's some code:
var client = new WebClient();
client.BaseAddress = "http://localhost/";
client.Headers["Content-type"] = "application/x-www-form-urlencoded";
client.Proxy = null;
// The server gets the data
var responseStr = client.UploadString(url, data);
...
// The server gets an empty post data
responseStr = client.UploadString(url, data);
Is Webclient supposed to be an disposable thing?
hmm may be u cant try this :
WebClient client = new WebClient();
client.BaseAddress = "http://localhost/something.php";

Domain credentials for a WebClient class don't work

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;

Categories