I'm have some problems to use webclient.
When I try it:
var client = new WebClient();
client.Credentials = new NetworkCredential("intranet.homolog", "S3br#32011", "na-sebrae");
var html = client.DownloadData("http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=");
I get an error (401).
This url returns xml feed, and, when I access it into browser, I login normally.
This user, and password are real.
Somebody have some ideia to I access it with the webclient?
Here's my guess: you're misusing the NetworkCredential constructor
The correct syntax is
public NetworkCredential(
string userName,
string password,
string domain
)
First username, then password, then domain - you got yours all wrong.
Try the following:
var client = new WebClient();
client.Credentials = new NetworkCredential("na-sebrae",
"S3br#32011", "intranet.homolog");
var html = client.DownloadData("http://www.intranet.sebrae.com.br" +
"/noticias/todas-as-notícias/rss.aspx?estado=");
I too get same error. The same link work better in browser but but giving 401 exception for WebClient.
string url = "http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=";
var webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
byte[] html = webClient.DownloadData(fileAbsoluteUri);
Related
I want to download Images from SharePoint Online site . Here is my code it will give webclient Exception.
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);
DownloadFile(url, credentials, "https://damasjewellery.sharepoint.com/:i:/r/Products/Catalogue%20Images/BDR-001-NA-RG-X-0.JPG?csf=1&e=FclkOs");
DownloadFile Method Contains Webclient Object And Its DownloadFIle method. When I pass Url and path of Particular Images It will Give me an exception .
using (var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
client.Credentials = credentials;
client.DownloadFile(webUrl, fileRelativeUrl);
}
A WebClient Exception usually means there's some kind of connection issue. Either the server returned a 404, the server timed out, there's no internet connection, or you're having a permissions issue.
*Edit: Realized the exception being thrown was already in the question.
Try swapping url to uri.
public void DownloadFile(
Uri address,
string fileName
)
I have a cURL command provided by WooCommerce:
curl https://example.com/wp-json/wc/v2/orders \
-u consumer_key:consumer_secret
I'm using WebClient:
using (WebClient wc = new WebClient())
{
Uri url = new Uri("https://www.myhost.com/wp-json/wc/v2/orders");
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("consumer_key", keyValue);
byte[] responsebytes = wc.UploadValues(url, "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
But I'm getting this error:
IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
What have I done wrong here? I'm pretty sure my url and key are correct.
EDIT: Have just tried this as suggested by CmdrTchort and David:
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
wc.Credentials = new NetworkCredential("consumer_key", keyValue);
string responsebody = wc.DownloadString("https://www.myhost.com/wp-json/wc/v2/orders");
}
Still receiving the same error.
EDIT: I have a suspicion I've been provided with invalid credentials I will update once this becomes clear...
I was given the wrong credentials. However that has not solved the issue.
I don't understand how consumer_key:consumer_secret is supposed to be represented in this request. There are two values: consumer_key and consumer_secret (I was only supplied with consumer_secret before, which I placed where keyValue is). I now assume that it is supposed to be of the form:
wc.Credentials = new NetworkCredential("consumer_key", "consumer_secret");
where "consumer_key" and "consumer_secret" represent the unique values provided. This does not work. This is starting to get slightly irritating.
You're posting the consumer_key:consumer_secret as part of a form POST, but that's not what cURL was doing. That looks more like a GET request with supplied authentication. Perhaps something like this:
wc.Credentials = new NetworkCredential("consumer_key", "consumer_secret");
and then making a GET request instead of a POST. With no values to upload, that part might be simplified. Maybe something like:
string responsebody = wc.DownloadString(url);
-u in curl passes the credentials as authorized headers and not as a dictionary of params.
You can add your credentials with :
wc.UseDefaultCredentials = true;
wc.Credentials = new NetworkCredential("username", "password");
Is your service responding and/or is it an untrusted or self-signed certificate?
If so , you can test ignoring the SSL-warning.
Btw, curl uses get by default (-X specifies method, which I can't see in your example) - so I'm assuming your only receiving by default and not actually posting values? If so you can use the DownloadString() directly.
So, if you're trying to get a file from this url you can do :
// IGNORE ALL SSL Certificates - would not do this in production
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
delegate
{ return true; }
);
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
wc.Credentials = new NetworkCredential("consumer_key", keyValue);
string responsebody = wc.DownloadString("https://www.myhost.com/wp-json/wc/v2/orders");
}
I have a HTTPS URL which i am trying to connect to but not able to and keep getting 401 unauthorized.
I tried to access the same URL over Web Browser (both at home and in company) and was able to connect to it. After i execute the HTTPS URL (which has some parameters in it like date etc) in the browser, it prompts for username and pwd and when i enter it, it gives the xml back in response.
I tried following segment of code on my home PC and it worked fine but when i tried same in office network, it gave me 407 error. I thereafter embedded the proxy code in it and now i don't get 407 rather i keep getting 401. Please help
Working Code on Home:
public static void WorkingCode()
{
string URL = "https://webservice.XYZ.com/display/?start_date=2015-05-06&end_date=2015-05-07";
Uri uri = new Uri(URL);
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username123", "Password123");
string results;
results = wc.DownloadString(uri);
Console.Write(results);
Console.Read();
}
Code within Organization with PROXY:
public static void WorkingCode()
{
string URL = "https://webservice.XYZ.com/display/?start_date=2015-05-06&end_date=2015-05-07";
Uri uri = new Uri(URL);
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("username123", "Password123");
string results;
/* PROXY CODE*/
WebProxy myProxy = new WebProxy("frproxyseczom.PPP.com", 8080);
myProxy.UseDefaultCredentials = true;
wc.Proxy = myProxy;
/*---------------*/
results = wc.DownloadString(uri);
Console.Write(results);
Console.Read();
}
Because of security reasons, i have modified some details such as URL's, username and pwd.
You're probably getting a 401 in the proxy code because you're missing "http://" in the web proxy address.
So basically I'm working on integrating a web API into my project from an externally hosted source.. But the xml is stored behind basic authentication. So I've been advised that I need to parse some authentication into the header of my HTTP request when contacting the location of the XML.
Here's what I'm working with at the moment:
I've created a controller, my code is as follows:
namespace com.tortoise.Controllers
{
public class VebraController : ApiController
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
string username = "user";
string password = "password";
string usernamePassword = ("username + : + password");
CredentialCache cache = new CredentialCache();
new cache.add Uri(url), "Basic", new class NetworkCredential(username, password));
request.Credentials = cache;
request.Headers.Add("Authorization", "Basic " // <- space here.
+ Convert.ToBase64String()(new Int64 ASCIIEncoding().GetBytes (usernamePassword));
// Get the token from the response:
string token = response.GetResponseHeader("Token");
}
Any help is great. I'm receiving errors in CredentialCache, ASCIIEncoding, ToBase64String(), GetBytes() and GetResponseHeader().
Alright, I have fixed your code a bit. It should compile now, assuming you have actually declared the variables you are using. I can't do that for you. Please read the comments. You seem to need a lot of practice with C# syntax and the language in general, you had a lot of invalid C# in there. Hope this helps.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string username = "user";
string password = "password";
//here I am declaring the NetworkCredentials. You do not need to put 'new class'
NetworkCredential myCredentials = new System.Net.NetworkCredential(username,password);
string usernamePassword = (username + password); //I assume you meant to concatenate them
CredentialCache cache = new CredentialCache();
cache.Add((Uri)url, "Basic", myCredentials); //you must declare url, not sure what you want it to be
request.Credentials = cache;
request.Headers.Add("Authorization", "Basic " // <- space here.
+ Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword)); //fixed this
// Get the token from the response:
string token = response.GetResponseHeader("Token"); //you need to declare response somewhere
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.