Quickest way to get StatusCode while posting data - c#

What i'm wanting to do it post data (ID=123456&start=true) and have it return the website status (200, 404, 401, etc.)
I don't want any other content except for the StatusCode, what is my best way of doing this?
I have been using
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://example.com/test.php");
request.Method = "POST";
But it seems to download the whole page before it gives me the StatusCode.
I have also tried:
request.Method = "HEAD";
But that wont let me send any post data with it.

Related

WebRequest.GetResponse(): read response, even on a 401?

I'm having trouble with the authorization piece of a third-party API. The API is shooting me a 401 in the HTTP Status Code, and more info in the response body.
var req = HttpWebRequest.Create(url);
req.Method = "GET";
var resp = req.GetResponse();
This is the code I typically use, but GetResponse() throws an exception on the 401 and I never get the response body.
Is there another implementation of this request I'm missing, or another way to do this in .NET so that I can get the response even when the request generates a 401?
Thanks.
web exception
The WebException contains a Response object that you can use to read the response body

C# read json data from url without using api

We are getting yammer data using yammer api however in our scenario we want a notification data but there is no api support for that, as an alternative we thought of reading the json content from a url, when we type this url in browser "https://www.yammer.com/******/notifications.json" we are getting data in the browser however through C# we are getting 406 error as there is no api support.
For api we use token id as oauth authenticating medium but to get content from a url in the form of json what authentication is available, in short please help with some code
Code we have used:
HttpWebRequest Request = HttpWebRequest.Create(URL) as HttpWebRequest;
Request.Method = "GET";
Request.ContentType = "application/json; odata=verbose";
Request.Accept = "application/json; odata=verbose";
string Token = ConfigurationManager.AppSettings["Token"];
Request.Headers.Add(HttpRequestHeader.Authorization, Token);
var wResp = (HttpWebResponse)Request.GetResponse();
If you have any alternative solution please help.

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.

C# REST client sending data using POST

I'm trying to send a simple POST request to a REST web service and print the response (code is below, mostly taken from Yahoo! developer documentation and the MSDN code snippets provided with some of the documentation). I would expect the client to send:
Request Method: POST (i.e. I expect $_SERVER['REQUEST_METHOD'] == 'POST' in PHP)
Data: foo=bar (i.e. $_POST['foo'] == 'bar' in PHP)
However, it seems to be sending:
Request Method: FOO=BARPOST
Data: (blank)
I know the API works as I've tested it with clients written in Python and PHP, so I'm pretty sure it must be a problem with my C#. I'm not a .NET programmer by trade so would appreciate any comments/pointers on how to figure out what the problem is - I'm sure it's something trivial but I can't spot it myself.
uri, user and password variables are set earlier in the code - they work fine with GET requests.
request = (HttpWebRequest) WebRequest.Create(uri);
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
string postData = "foo=bar";
request.ContentLength = postData.Length;
StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
postStream.Write(postData);
postStream.Close();
response = (HttpWebResponse) request.GetResponse();
The REST API is written in PHP, and the $_POST array is empty on the server when using the C# client.
Eventually found the HttpWebRequest.PreAuthenticate property which seems to solve the problem if the code is edited like so:
request = (HttpWebRequest) WebRequest.Create(uri);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;
From the documentation I presume this forces authentication before the actual POST request is sent. I'm not sure why the class doesn't do this automatically (libraries for other languages make this process transparent, unless you explicitly turn it off), but it has solved the problem for me and may save someone else another 2 days of searching and hair-pulling.
For what it's worth, PreAuthenticate doesn't need to be set for GET requests, only POST, although if you do set it for a GET request everything will still work, but take slightly longer.

How to read HTTP header from response using .NET HttpWebRequest API?

My app currently uses OAuth to communicate with the Twitter API. Back in December, Twitter upped the rate limit for OAuth to 350 requests per hour. However, I am not seeing this. I am still getting 150 from the account/rate_limit_status method.
I was told that I needed to use the X-RateLimit-Limit HTTP header to get the new rate limit. However, in my code, I do not see that header.
Here is my code...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newURL);
request.Method = "GET";
request.ServicePoint.Expect100Continue = false;
request.ContentType = "application/x-www-form-urlencoded";
using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
}
If I inspect the response, I can see that it has a property for Headers, and that there are 16 headers. However, I do not have X-RateLimit-Limit in the list.
(source: yfrog.com)
Any idea what I am doing wrong?
You should simple be able to use:
using (WebResponse response = request.GetResponse())
{
string limit = response.Headers["X-RateLimit-Limit"];
...
}
If that doesn't work as expected, you can do a watch on response.Headers and see what's in there.
Look at the raw response text (e.g., with Fiddler). If the header isn't there, no amount of C# code is going to make it appear. :) From what you've shown, it seems the header isn't in the response.
Update: When I go to: http://twitter.com/account/rate_limit_status.xml there is no X-RateLimit-Limit header. But when I go to http://twitter.com/statuses/public_timeline.xml, it's there. So I think you just need to use a different method.
It still says 150, though!

Categories

Resources