Get only response headers [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
HttpWebResponse: closing the stream
Using ASP.NET is it possible to make a request and get only response headers? I have to do a request to a big file, but I only need the response headers, i dont care about the content of the file.
I would like to know if there is something similar to get_headers from php (http://php.net/manual/en/function.get-headers.php).

I'm not sure how to do this natively, but at a minimum, you could use a custom HTTP handler (ASHX) file to create the headers you need, and return nothing else in the response.
Update:
If you set WebRequest.Method = "HEAD" then the server should automatically only return the Headers. This is according to W3.

Related

Best way to send HTTP requests in an ASP.NET webforms project c# [duplicate]

This question already has answers here:
How to simulate HTTP POST programmatically in ASP.NET?
(5 answers)
Closed 5 years ago.
Hey guys im a newbie working with HTTP requests in C#/.Net and would love some tips and suggestions of the correct namespaces or classes that would make doing HTTP requests easier. GET,POST,.etc
Ive come across a few such as the HTTPrequest class, and also the HTTP.WebRequest but im lost as to which to actually use and any advice would be welcome!
The short answer is that you have to make use of HttpWebRequest to make the request and HttpWebResponse to get the response. But this will probably not solve your problem as the configurations for making HTTP requests differ from one case to another. For example, headers, content type, and accept are parameters to which you have to pay attention based on your method (GET, POST,PUT,PATCH,DELETE) and the API with which you are communicating.
For example, let's say that you wanna send and receive a text message, you should follow something like this:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("URL");
webRequest.ContentType = "text/plain;charset=UTF-8";
webRequest.Accept = "text/plain;charset=UTF-8";
webRequest.Method = "POST";
using (StreamWriter requestStream = new StreamWriter(webRequest.GetRequestStream())) {
requestStream.WriteLine("Hello");
}
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream())) {
Console.WriteLine(responseStream.ReadToEnd());
}
You have to keep in mind that your request type (ContentType) or your response type (Accept) may be in different formats such as XML or JSON or even Stream and you have to find the correct settings for your case. You may also need to add custom HTML headers in your request.

Sending a POST request to a URL from C# [duplicate]

This question already has answers here:
Send HTTP POST request in .NET
(16 answers)
Closed 5 years ago.
I have a Node server hosted with Azure, where I can send a POST request to the API for it to perform some function. The API itself works, I have tested it with Post Man.
A call to the API would look something like this..
http://website.com/api/Foo?name=bar&second=example
This doesn't necessarily need to return anything, as the call is silent and does something in the background. (note: perhaps it must return something and this is a hole in my understanding of the concept?)
Using C#, how can I make a web request to this URL?
I am already constructing the URL based on parameters passed to my method (so name and type as above could be whatever was passed to the method)
It's the POSTing to this URL that I cannot get working correctly.
This is the code I have tried..
void MakeCall(string name, string second)
{
string url = "http://website.com/api/Foo?name="+name+"&second="+second;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = url.Length;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
You have to create a request stream and write to it, this link here has several ways to do this either with HttpWebRequest, HttpClient or using 3rd party libraries:
Posting data using C#

Make a request to StackExchange Api [duplicate]

This question already has answers here:
.NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses?
(3 answers)
Closed 7 years ago.
I'm trying to request list of tags on StackExchange in JSON format by url, but problem is, that I'm getting some broken text instead of JSON, so I can't even parse it.
P.S. Done it with the help of RestSharp.
private void Refresh()
{
var client = new RestClient("http://api.stackexchange.com/2.2/tags?order=desc&sort=popular&site=stackoverflow");
var result = client.Execute(new RestRequest(Method.GET));
var array = JsonConvert.DeserializeObject<Root>(result.Content);
Platforms = array.Platforms;
}
If you make GET request to this URL using Fiddler, you will see that response has a header:
Content-Encoding: gzip
Which means that response is compressed with gzip. Good news is that HttpWebRequest can handle that:
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
After you add this row you will get nice and readable JSON.
As #peeskillet mentions, this looks like compressed data. Please have a look at
What is the canonical method for an HTTP client to instruct an HTTP server to disable gzip responses? and especially this answer.
Something like
Accept-Encoding: *;q=0
should help.

What's the fastest way to get size of an online file [ http ] [duplicate]

This question already has answers here:
How to get the file size from http headers
(5 answers)
Closed 9 years ago.
I'm working on a YouTube Downloader, which gets sizes of all qualities available but usually that takes a very long time and sometimes it just goes like a lightning !
I wrote this Function and it's pretty good [ only sometimes as I said before ] !
Uri url = new Uri("http://example.com/document.docx");
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
response.Close();
// iSize will handle the size of the file in bytes
Int64 iSize = response.ContentLength;
.
So, Is there a faster way to do this ? ( Put on your mind that the files which will be measured are more than eight files ) .
.
In advance, thank you so much ;
That completely depends on how well-behaved the HTTP server is.
The specification says that you can send a HEAD request to get response headers like Content-Length without serving the file.
However, many servers don't do that.

How to get the content of web page? [duplicate]

This question already exists:
Closed 11 years ago.
Possible Duplicate:
Reading web page by sending username & password?
My problem is this. There is a site that has data which is frequently updated that I would like to get at regular intervals for later reporting.
for getting that data i have to provide the userid & password.
I have used HttpWebRequest to get data but the problem is that response text returns "Your browser doesn't support frame" instead of the data i want.
how can i get it?
Most likely you are having this problem because you are not setting the user-agent in your request, i.e. with a WebClient:
using(WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
string htmlResult = wc.DownloadString(someUrl);
}
You can make use WebBrowser control to solve your problem. This approach works like this, First, you have to load the specific webpage on to the WebBrowser Control, then once the document has been loaded or not . If loaded then you can retrieve the web page stream using DocumentStream property.
Hope this helps.

Categories

Resources