HTTP GET works in browser, but doesn't with HTTPWebRequest - c#

Title explains it. I am trying to change the password of a webpage programmatically using HTTP Web Request. When I do it through Google Chrome (pasting the uri in the address bar), the password change works. Here is the header I get when I inspect in Chrome.
Remote Address:10.160.70.55:443
Request URL:https://10.160.70.55/cgi-bin/check_user.cgi?Type=basic&Current=78-62-118-112-106-108-56&Password=98-96-102-96-106-96
Request Method:GET
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fr;q=0.6,ar;q=0.4
Authorization:Digest username="admin", realm="Secure Access", nonce="f3c6fcd9a549a42e9aac22818cb0f5ad", uri="/cgi-bin/check_user.cgi?Type=basic&Current=78-62-118-112-106-108-56&Password=98-96-102-96-106-96", response="cd17523a279f044d086b5bd0245eda0e", qop=auth, nc=0000001b, cnonce="9bd719c27efd9721"
Connection:keep-alive
Host:10.160.70.55
When I try to do it programatically, I receive the OK (200) code, but the password change does not work. Here's my code.
System.Uri uri2 = new Uri(string.Format("https://{0}/cgi-bin/check_user.cgi?Type=basic&Current=78-62-118-112-106-108-56&Password=98-96-102-96-106-96", ip));
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(uri) as HttpWebRequest;
request2.Accept = "text/html";
request2.Credentials = new NetworkCredential("admin", "M#ster1");
request2.Method = WebRequestMethods.Http.Get;
HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
WriteLog(response2.StatusDescription.ToString());
response2.Close();

Used fiddler as suggested, and it turns out it was the user-agent header. And thank you for correcting the typo L.B. :)

Related

C# HttpWebRequest Medium Blog returns 403 Forbidden but Site is Open

Medium blog pages are available on Chrome, IE etc... browsers but I can not send a web request with this code blog. It returns 403 Forbidden. By the way this method was working properly a couple of days ago. I changed my IP address numerious times, thought they might have banned my IP address but did not work.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://medium.com/#coinbaseblog");
request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (System.IO.Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
Try this:
var request = (HttpWebRequest)WebRequest.Create("https://medium.com/#coinbaseblog");
request.UserAgent = #"Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)";
var result = (HttpWebResponse)request.GetResponse();
http status code 403 indicates that server has understood the request but refuses to authorize it. Even if you are reauthenticating it will make no difference. This is similar to http status code 401 but in this case reauthentication works just fine. You may be authenticated but the resource which you are trying to access is restricted for you.

Yelp Fusion API v3 returns 401 Unauthorized with TOKEN_MISSING error when called from c#

I've created my app in Yelp, got my api key, and things work fine from Postman when executing a business search.
However, when testing from c#, I receive a 401 unauthorized error with a TOKEN_MISSING error that says ""{\"error\": {\"code\": \"TOKEN_MISSING\", \"description\": \"An access token must be supplied in order to use this endpoint.\"}}"".
I'm supplying my api key correctly though, and the Yelp documentation says that's all I need, so I'm not sure what the problem is. Here are 2 separate c# code samples that do NOT work (I've replaced my actual api key with for security concerns):
Example using WebRequest:
var webRequest = WebRequest.Create("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
webRequest.Method = "GET";
webRequest.Headers.Add("Cache-Control", "no-cache");
webRequest.Headers.Add("Authorization", "Bearer <my_api_key>");
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
var stream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
var content = stream.ReadToEnd();
Console.Write(content);
Example using RestSharp:
var client = new RestClient("http://api.yelp.com/v3/businesses/search?term=Clayton+Bicycle+Center&location=5411+Clayton+Rd%2c+Clayton%2c+CA+94517%2c+US");
var request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Bearer <my_api_key>");
var response = client.Execute(request);
Console.Write(response.Content);
I've examined the requests in Fiddler, and both are sending the same headers as the working Postman search, but both return 401 unauthorized error while Postman returns the search results. Any ideas?
Edit:
Well this is embarrassing, apparently my issue was I was attempting to access the Yelp API via http instead of https. Once I changed to https, everything worked as expected.
Changed endpoint to use https instead of http, works now.

Consuming REST Webservice. API Key Authentication

I have little problem that I have been trying to solve for some time. I want to connect to a REST web service and I have the API key for that web service that I want to consume. I have tried the service in the Google REST console and it works fine.
But when I try to build a c# .net project for it I dont know how to set the api key for the authentication. I took this code from other site:
string url = "http://Demo.company.com/Data/Values/1029/CarPart/id/"
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
Console.WriteLine("Sending GET Request");
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);
Console.WriteLine("Response from Server");
Console.WriteLine(sr.ReadToEnd());
How can I authenticate this service with my Api key: asdf1234. I need to add it to my header but how? Can you show me some code example?
Cheers
Thor
You should be able to do something like:
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
GETRequest.Headers.Add("api-key", "asdf1234");
Where "api-key" is the name of the header you want to set.

HttpWebRequest with GET: 401 unauthorized

I found some problem with httpWebRequest, I've read all the same issues on other forums, but answers don't seem to work. My code:
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp;
wr.ContentType = "text/html; charset=UTF-8";
wr.Method = "GET";
wr.Credentials = new NetworkCredential("user", "password");
resp = (HttpWebResponse)wr.GetResponse();
The remote server returned an error: (401) Unauthorized.
Response says there's no auth token in cookies. I can receieve this token using my auth request with POST method. I even tried to put it to CookieContainer by "new Cookie ("authToken",token_value)". But the result is the same - error 401. Does anybody know the solution?
Thanx.
I use Zimbra web server, have an access to control it. .NET 4.0. My url is the path to .eml file I need to download. To specify the file I need to add some GET parameters: id and part. So the whole address looks like http://someserver.info/service/content/get?id=1&part=1
(Answered in the comments and question edits by OP. Moved here. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
The authorization token in Zimbra called ZM_AUTH_TOKEN so you need to put your authtoken in cookies like this:
wr.CookieContainer = new CookieContainer();
wr.CookieContainer.Add(new Uri(url), new Cookie("ZM_AUTH_TOKEN", rc.AuthToken));
You don't need to put the auth headers then, the request will work

Weird paypal api issue

I just started using the Paypal API and I'm stuck on this problem.
I generate a paypal request in code and when I send it I get back the following.
TIMESTAMP=2011-05-16T01:26:37Z
CORRELATIONID=6d4327d15421f
ACK=Failure
L_ERRORCODE0=10001
L_SHORTMESSAGE0=Internal Error
L_LONGMESSAGE0=Timeout processing request
When I run through the debugger and copy the generated request url and paste it into my web browser, I get a success response....
I'm sending the request like this - c#
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
WebResponse response = req.GetResponse();
The same request, but one sent by code, and one copied to the browser produces to different results. Why is that?
I got same problem. When remove the
request.Method ="POST";
line, problem is solved.

Categories

Resources