How to use C# .net 4 to get webserver header time - c#

In C# on .net 4.0 I'm trying to get the header date/time of a Internet web site.
My goal is to validate a local systems time (within seconds) of Internet time by using HTTP rather then SNTP. I'm a SNTP fan but it won't do in this scenario. I found this concept of using HTTP headers for time called "HTP" and want to replicate it in C#.
Tried to use HttpWebRequest.Headers collection using MSDN example on the page, which doesn't return me the Date (or much else).
If HttpWebRequest.Headers is a good way to go about getting this value, why can't I see Date in this result? Is there a better way?
var myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",
myHttpWebRequest.Headers);

You seem to be reading the request headers, instead of reading the response headers:
var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
var response = myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}", response.Headers);

Related

Elasticsearch scroll when using a simple HTTP POST method in a C# console program

I have a C# console program which is retrieving a large amount of data in raw JSON format. This work well using this code:
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
client.Headers["Content-Type"] = "application/json";
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials));
string myDataResult = client.UploadString(url, "POST", myQuery);
<result handling code>
}
At the moment there is no maximum size, but in a short while there will be a limit of 3000. This means I have to use scrolling to get all the data I need. I have been reading about NEST, but I hope to avoid it. I just need the raw JSON output my request i producing. Any simple ways to extend my code so it uses scrolling?
Best regards, Brian
You will need to maintain and pass in to your request what page you're requesting, and the limit (number of items you want returned).
I would take a look at the ElasticSearch API docs for examples of how the request body would look. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
Going off of this, it looks like "from" would be the page you're requesting, "size" would be the limit or number of items you want.

Translating this to a HTTP POST in C#

I am currently experimenting with the HTTP request. I have successfully managed to do get requests and I have read on doing post request with HTTP request.
Now I am trying to work with the yahoo API and in order to use the Yahoo api it states that at
The Message Management API can be used to send a message to another
Yahoo! Messenger contact. The API is very simple to use, as shown
here. Note that the contact that the message is sent to is part of the
URI, using the following format:<server>/v1/message/<network>/<contactID>
POST /v1/message/yahoo/targetYahooId?sid=msgrsessionid
Host: rcore1.messenger.yahooapis.com
Authorization: < Standard OAuth credentials >
Content-Type: application/json;charset=utf-8
Content-Length: 25
{
"message" : "Hey there"
}
Now I have an OAuth string which I obtained from get using the HttpWebRequest object.
The string is something like this
oauth_token=A%3Dvh....aRg--&oauth_token_secret=bd46a....c9239&oauth_expires_in=3600&oauth_session_handle=ALtT.....3J1N4Zg--&oauth_authorization_expires_in=784964948&xoauth_yahoo_guid=TUSKED5...NCIA
UPDATE
Now my question are as follows :
1- If I am using WebRequest object in C# what would my URI look like
2- I understand that it requires a JSON type object. How do i even know what OAuth parameters are ?
One thing you'll need to change is the content type:
request.ContentType = "application/json;charset=utf-8";
And of course, the url.
you need to change the url on the line with the url in it
you need to change the content-type line
you need to make the payload into a json string then convert it to a byte array (byteArray in the sample)
either assemble the json by hand "{ foo:'bar'}" etc or use json.net
and set the content-length
Looks like it's expecting a JSON object for the request body. Depending on the version of .NET you're using, you can either use a Javascript serializer as shown here (https://stackoverflow.com/a/7003815/939080) or JSON.NET (http://james.newtonking.com/projects/json-net.aspx) to convert your form collection into JSON output.
You are asking an open-ended question that would require people to write a bunch of code for you if you want a specific and complete answer. As others have pointed out, there are several issues that you'd need to deal with:
The JSON payload, which would be a straightforward matter of putting the JSON string in the request body via the byteArray used in the code sample.
The content type, which you would need to change as described by jrummell.
The OAuth credentials, which is a kettle of fish you'll need to read about, understand, and acquire a library for. Here's a good place to start looking for a library.

Send back an HTTP Response without receiving a Request

I have a C# server that will accept connections from a game/client and return information from a database to the game/client.
The client posts an xml file. I would like to send a 200 OK Response back to the client.
I have tried numerous things. Can anyone suggest something. I also need the response to HTTP 1.0.
Thanks.
If you're looking to write an application that serves web requests with responses and want it to be (relatively) lightweight, at least in regards to IIS, try looking into the Kayak Project. Seems pretty close to what you're aiming to do.
This thread in the mailing list seems especially relevant.
Your question is so confusing... "send back" without request in the title... but a request is done when you explain later the full problem...
Anyway, I don't know if this is what you want to do, but can send your own headers using the Status and StatusCode properties of the HttpResponse class.
So I didn't really understand the http response. I forgot to send back the two CRLF at the end of the response. Thanks alot.
--definitions: CRLF = "\r\n"; s = NetworkStream().
StringBuilder str = new StringBuilder();
str.Append("HTTP/1.0 200 OK\r\n");
str.Append("Date: Tue, 17 Aug 2011 11:40:00 EST\r\n");
str.Append("Server: Apache\r\n");
str.Append("Content-Type: text/xml\r\n");
str.Append("Content Length: 128\r\n");
byte [] r = Encoding.ASCII.GetBytes(str.ToString() + CRLF+ CRLF);
s.Write(r, 0, r.Length);

What is a structure of a standard POST request

I am using C# for my project, can anyone tell me what is a standered structure of a HTTP POST Requset. How to attach POST data like a file in the Request from code.
Simply i want to create a POST request from my code itself, with diffrent items to be posted available.
I have checked Ietf's RFC for http POST but its too long....
Specs for simple reference
I have always appreciated HTTP Made Really Easy as a starting point. It's small, concise and friendly.
Often you can get enough implementation details (or at least enough understanding) from this guide's simple style to suffice your need. It has worked for me many times. There is a section on POST. The guide builds cumulatively.
Additionally it links to proper specifications and fuller resources should you need to reference them and get into more detail.
.NET Supporting Classes
Fortunately the .NET Framework Class Library contains higher level classes that can simplify your life. Look into the MSDN documentation and examples about System.Net.WebClient (doesn't lend itself as well to POST, favours GET for quick usage methods). Consider the more flexible System.Web.HttpRequest and System.Web.HttpResponse counterpart classes.
Example using C#
This code sample shows the concept of posting binary data to a stream.
This method is called like:
PostMyData(Stream_instance, "http://url_to_post_to");
Namespaces involved are:
using System.IO;
using System.Net;
The custom method would look something like the following.
Note: Concept taken from MSDN sample code here.
Although I use MIME type application/octet-stream for generic binary data, you can use any well known type from this list of mime types to target the kind of binary data you are sending.
public int PostMyData(Stream binaryData, string postToUrl) {
// make http request
var request = (HttpWebRequest)WebRequest.Create(postToUrl);
request.Method = "POST";
request.ContentType = "application/octet-stream"; // binary data:
// data (bytes) that will be posted in body of request
var streamOut = request.GetRequestStream();
binaryData.CopyTo(streamOut);
// post and get response
using (var response = (HttpWebResponse)request.GetResponse()) {
var code = response.StatusCode;
return (int)code;
}
}
Use HttpWebRequest, its always the best way, but for a more simple aproach on Http Post read:
http://programaticallyspeaking.site40.net/blog/2010/11/how-to-implement-an-http-server-part-1/
Hey i found a way to view a sample POST request, Use Fiddler to track HTTp transfers and click on RAW to view raw data being transfered.

HTTP Post in C# console app doesn't return the same thing as a browser request

I have a C# console app (.NET 2.0 framework) that does an HTTP post using the following code:
StringBuilder postData = new StringBuilder(100);
postData.Append("post.php?");
postData.Append("Key1=");
postData.Append(val1);
postData.Append("&Key2=");
postData.Append(val2);
byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString());
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://example.com/");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = dataArray.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(dataArray, 0, dataArray.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse webResponse = (HttpWebResponse)httpRequest.GetResponse();
if (httpRequest.HaveResponse == true) {
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
String responseString = responseReader.ReadToEnd();
}
The outputs from this are:
webResponse.ContentLength = -1
webResponse.ContentType = text/html
webResponse.ContentEncoding is blank
The responseString is HTML with a title and body.
However, if I post the same URL into a browser (http://example.com/post.php?Key1=some_value&Key2=some_other_value), I get a small XML snippet like:
<?xml version="1.0" ?>
<RESPONSE RESULT="SUCCESS"/>
with none of the same HTML as in the application. Why are the responses so different? I need to parse the returned result which I am not getting in the HTML. Do I have to change how I do the post in the application? I don't have control over the server side code that accepts the post.
If you are indeed supposed to use the POST HTTP method, you have a couple things wrong. First, this line:
postData.Append("post.php?");
is incorrect. You want to post to post.php, you don't want post the value "post.php?" to the page. Just remove this line entirely.
This piece:
... WebRequest.Create("http://example.com/");
needs post.php added to it, so...
... WebRequest.Create("http://example.com/post.php");
Again this is assuming you are actually supposed to be POSTing to the specified page instead of GETing. If you are supposed to be using GET, then the other answers already supplied apply.
You'll want to get an HTTP sniffer tool like Fiddler and compare the headers that are being sent from your app to the ones being sent by the browser. There will be something different that is causing the server to return a different response. When you tweak your app to send the same thing browser is sending you should get the same response. (It could be user-agent, cookies, anything, but something is surely different.)
I've seen this in the past.
When you run from a browser, the "User-Agent" in the header is "Mozilla ...".
When you run from a program, it's different and generally specific to the language used.
I think you need to use a GET request, instead of POST. If the url you're using has querystring values (like ?Key1=some_value&Key2=some_other_value) then it's expecting a GET. Instead of adding post values to your webrequest, just put this data in the querystring.
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://example.com/?val1=" + val1 + "&val2=" + val2);
httpRequest.Method = "GET";
httpRequest.ContentType = "application/x-www-form-urlencoded";
....
So, the result you're getting is different when you POST the data from your app because the server-side code has a different output when it can't read the data it's expecting in the querystring.
In your code you a specify the POST method which sends the data to the PHP file without putting the data in the web address. When you put the information in the address bar, that is not the POST method, that is the GET method. The name may be confusing, but GET just means that the data is being sent to the PHP file through the web address, instead of behind the scenes, not that it is supposed to get any information. When you put the address in the browser it is using a GET.
Create a simple html form and specify POST as the method and your url as the action. You will see that the information is sent without appearing in the address bar.
Then do the same thing but specify GET. You will see the information you sent in the address bar.
I believe the problem has something to do with the way your headers are set up for the WebRequest.
I have seen strange cases where attempting to simulate a browser by changing headers in the request makes a difference to the server.
The short answer is that your console application is not a web browser and the web server of example.com is expecting to interact with a browser.
You might also consider changing the ContentType to be "multipart/form-data".
What I find odd is that you are essentially posting nothing. The work is being done by the query string. Therefore, you probably should be using a GET instead of a POST.
Is the form expecting a cookie? That is another possible reason why it works in the browser and not from the console app.

Categories

Resources