I have a tiny app that I wanting to run and ping an internal web site. Here is the code:
using (var client = new WebClient())
{
client.DownloadString("http://MyServer/dev/MyApp");
}
However, it is throwing the following error:
The remote server returned an error: (401) Unauthorized.
I have all the correct credentials to access the server. I am thinking I don't know how to use WebClient very well and I just need to set properties on the client object. Any ideas?
I found the answer. I needed to use the NetworkCredentials() method of WebClient. See below:
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential ("theUser", "thePassword", "theDomain");
client.DownloadString("http://MyServer/dev/MyApp");
}
This is the URL that helped me
Related
Am using WebRequest to validate Url before saving it to Db, But for some url's am getting 403 forbidden error. Please suggest me how can i resolve this issue or any alternate way for this.
i have used code like below.
var webRequest = WebRequest.Create(url);
var webResponse = webRequest.GetResponse();
i also tried WebClient in local its working but when we deploy its throwing same error. Please find the WebClient code aswell
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("user-agent", "test");
string content = webClient.DownloadString(url);
}
The main purpose we are used this to check when user enters any url attacks.. Please suggent if any way to resolve this
I'm migrating my existing console app to a web app. I'm making an api call in the controller. The below code works perfectly in console app. But in web app,i get exception at
var result = webClient.DownloadString(sourceUrl);
with the message "unable to connect to remote server" and the inner exception says {"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond}System.Exception {System.Net.Sockets.SocketException}
I have tried to pass the url as uri object, edited web.config file to include default credentials, servicepointmanager etc. I have also tried to make it asynchronous using async and await. I really do not know what is going on. I'm behind a corporate proxy but the api call works perfectly in console app and Postman.
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
namespace webApp.Controllers
{
public class DataController : ApiController
{
private string sourceUrl="http://api.openweathermap.org/data/2.5/weather?zip=94040,us&appid=YOURID";
[Route("")]
public void GetAlldata()
{
var dataCall = DownloadData(sourceUrl);
}
private string DownloadData(string url)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var webClient = new WebClient();
IWebProxy wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials;
webClient.Proxy = wp;
var result = webClient.DownloadString(sourceUrl);
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}
Just to mention that after trying the above code in my home network and it worked, I pinpointed the problem to be a proxy issue. Removing the below from my code
IWebProxy wp = WebRequest.DefaultWebProxy;
wp.Credentials = CredentialCache.DefaultCredentials;
webClient.Proxy = wp;
and explicitly adding the below to web.config fixed it. I don't know why web app doesn't pickup the default proxy from code but console app does. Any explanation for this behavior will be appreciated.
<system.net>
<defaultProxy>
<proxy usesystemdefault="False" proxyaddress="http://proxy.MYCOMPANY.com:8080" bypassonlocal="True" />
</defaultProxy>
</system.net>
Íf above solution(cmoe's answer) don't work for your case. May be The console app pass proxy but other app drop by firewall. Check firewall settings.
I'm using WebClient to get the content of a specific page. When I try this with the code below, I get a syntax error which says The remote server returned an error: (401) Unauthorized.
I'm running my project on localhost (using Visual Studio 2015 Community and ASP.Net) and trying to get the content of this page "http://admin:admin#192.168.0.100/Monarch/syncconnect/sdk.aspx?command=GetStatus". I don't think you guys can see it, but this is basically all that the page contains:
<body>ENC1:RTSP,READY,ENC2:RECORD,READY,NAME:Monarch HDX</body>
My computer and that page both are connected to the same router (if that is usefull information).
How do I work around this error? Or is there a better way to get the content?
Code
var client = new WebClient();
string downloadString = client.DownloadString("http://admin:admin#192.168.0.100/Monarch/syncconnect/sdk.aspx?command=GetStatus");
loaded.Text = downloadString;
Try this:
var client = new WebClient();
client.Credentials = new System.Net.NetworkCredential("admin", "admin", "");
string downloadString = client.DownloadString("http://192.168.0.100/Monarch/syncconnect/sdk.aspx?command=GetStatus");
loaded.Text = downloadString;
Try setting
webClient.UseDefaultCredentials = true;
I am trying to connect to rackspace using their api and passing my username and api key but i get this error :
The remote server returned an error: (401) Unauthorized.
here is my code :
UserCredentials userCreds = new UserCredentials("myusername", "myapikey");
Connection connection = new Connection(userCreds);
I have followed this tutorial :
http://www.rackspace.com/knowledge_center/index.php/Sample_CSharp_Application
have asked their support and they say we can connect with same key using curl...and they couldnt provide much help.
anyone has any idea?
thanks
for anyone else who got same problem here i found the solution, you basically need to include the api uri :
http://blog.chmouel.com/2011/01/04/how-to-use-the-rackspace-cloud-uk-api/
Your sample works but I was looking for a direct way, without the wrapper. This seems to work as well, and uses direct restful access to the Rackspace API.
Hope it helps. Cheers.
string url = "https://auth.api.rackspacecloud.com/v1.0";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-Auth-User:" + userName);
request.Headers.Add("X-Auth-Key:" + apiKey);
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
string[] keys = response.Headers.AllKeys;
foreach (var k in keys)
Console.WriteLine(response.Headers[k]);
}
I'm attempting to use Fogbugz's BugzScout in order to automatically submit unhanded application exceptions to my Fogbugz on demand Account. I've written up a wrapper class for it and everything appears to be just groovy - on my box. Testing the same code in the production environment, behind a Proxy that requires authentication, I have had nothing but issues.
I went to work modifying the BugzScout code in order to get it to authenticate with the Proxy, and after trying many different methods suggested via a Google search, found one that works! But now I'm getting an "Connection actively refused" error from Fogbugz itself, and I don't know what to do.
Here is the code where the BugzScout connects via a .net WebClient to submit a new case, with my modifications to deal with our Proxy. What am I doing that would cause Fogbugz to refuse my request? I've removed all non web-client related code from the procedure for ease of reading.
public string Submit(){
WebClient client = new WebClient();
WebProxy proxy = new WebProxy();
proxy.UseDefaultCredentials = true;
client.Proxy = proxy;
Byte[] response = client.DownloadData(fogBugzUrl);
string responseText = System.Text.Encoding.UTF8.GetString(response);
return (responseText == "") ? this.defaultMsg : responseText;
}
The url is correct and the case is filled in properly- this has been verified.
EDIT: Additional info.
Using Fogbugz on Demand.
Using FogBugz.net code in it's entirety, with only these additions
WebProxy proxy = new WebProxy();
proxy.UseDefaultCredentials = true;
client.Proxy = proxy;
Error occurs when attempting to connect to both https://oursite.fogbugz.com/scoutsubmit.asp and http://oursite.fogbugz.com//scoutsubmit.asp (except one says port 443, and the other port 80, obviously)
I don't know anything about web authentication so I can't tell you what kind I'm using- if you tell me where to look I'd be happy to answer that for you.
Got the fix from Fogbugz- this is the appropriate network code to get though the proxy authentication and not mis-authenticate with Bugzscout.
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
WebRequest request = WebRequest.Create(fogBugzUrl);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
Is your fogbugzUrl using HTTP Basic Authentication? Is it SSL (hosted on On Demand?)
The connection actively refused message would be coming from the web server itself, not really FogBugz.
Can you post the HTTP Status Code?
One thing to note if you are using FogBugz On Demand is you HAVE to use the https:// url (not the http url).