Unauthorized to use WebClient on page - c#

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;

Related

The remote server returned an error: (403) Forbidden in WebRequest.GetResponse Method for some url's

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

Webexception:remote server returned an error (401)unauthorized

Giving Exception on
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
this line.And Error message is
remote server returned an error 401 unauthorized
It occures only when am calling web service from other project solution...
This exception is not occuring when calling service from same project solution.
what should I do to remove this exception and get Response from Remote server??
Plz help me.
Possible solutions can be:
1) add <identity impersonate="true"></identity> in web.config
2) If you have credentials
using (client = new MyWebService())
{
var username = ConfigurationManager.AppSettings["WSUserName"]
var password = ConfigurationManager.AppSettings["WSPassword"]
client.Credentials = new NetworkCredential(username, password);
// .. and the call here ..
}
Let me know if it doesn't fix your problem.

Get And Use Facebook Access Token Without HTTPS

I have the following url for the first stage. getting code http://www.facebook.com/dialog/oauth?client_id=myappid&redirect_uri=myurl;state=e879888c-7090-4c09-98a5-ff361b30d55;scope=email
and
url = String.Format(#"http://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}",FacebookAppId, redirectUrl, FacebookSecret, code);
WebClient webClient = new WebClient();
var tokenVars = webClient.DownloadString(url);
var responseVars = HttpUtility.ParseQueryString(tokenVars);
string access_token = responseVars["access_token"];
string data = webClient.DownloadString(String.Format("http://graph.facebook.com/me?access_token={0}", access_token));
var jobject = JObject.Parse(data);
400 BAD REQUEST is returned.
Question: Is it possible to get facebook access token without connecting by http s ?
Question: Is it possible to use access_token without HTTPS? NOT HTTP
for a secure you must take https,
maybe if you aren't, some bad thing will show, #long process..
i just browse for your issue, and same thing

Using WebClient to ping a web site

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

web service - 400 bad request

i want to consume a php Webservice from C# which is protected by htaccess.
i added the Service by adding a web reference in my VS 2010.
mywsfromwsdl myws = new mywsfromwsdl();
System.Net.CredentialCache myCredentials = new System.Net.CredentialCache();
NetworkCredential netCred = new NetworkCredential("user", "pass");
myCredentials.Add(new Uri(myws.Url), "Basic", netCred);
myws.Credentials = myCredentials;
myws.PreAuthenticate = true;
tbxIN.Text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> "+
" <test> "+
" <parm1>4</parm1> "+
" <parm1>2</parm1> "+
" </test>";
tbxOUT.Text= myws.func1(tbxIN.Text.ToString());
The VS shows an error called 400 Bad REquest my last row.
If i delete the .htaccess File on the server, the pgm works fine, but i cant delete. because other PHP User use the Service.
Can anybody tell me how to send the Credentials correctly ?
By jo
Sometimes C# and Apache clash a bit: in this case, it might be that your client is expecting a 100 Continue response due to authentication being active, but the server doesn't send it.
This kind of behavior is toggled by this line:
ServicePointManager.Expect100Continue = false;
Add it before executing the request.
It's also worth pointing out that when 400 bad request happens, you might find some useful details in server's logs.
According to MSDN you shouldn't need the credential cache:
// Set the client-side credentials using the Credentials property.
ICredentials credentials = new NetworkCredential("Joe",SecurelyStoredPassword,"mydomain");
math.Credentials = credentials;
Have you tried this method instead of the cache object? More info can be found here.

Categories