I'm aware that HttpWebRequest has a parameter available to set URL redirection to false (request.AllowAutoRedirect = False;).
How is this done when using a Socket connection directly?
I don't have any code to show since I'm just starting down the road of development on a project.
Unfortunately, I need to stick to using Socket connections and cannot use HttpWebRequest, WebClient or HTTPClient. :(
There is no such thing as a socket redirection.
An HTTP redirection is where you connect to the server specified, send your HTTP request, and server sends a HTTP response which tells you to go to a different URL instead. So then you go that URL instead.
It looks like this:
*** socket opened to example.com
*** client sends this part:
GET /some/address HTTP/1.1
Host: example.com
*** server sends this part:
HTTP/1.1 302 Found
Location: https://other.example.com/some_place?really=yes
*** socket closed to example.com
if you have auto-redirect disabled, this is the response your program gets. Otherwise, the HTTP library keeps going:
*** socket opened to other.example.com
*** client sends this part:
GET /some_place?really=yes HTTP/1.1
Host: other.example.com
*** server sends this part:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 11
hello world
*** socket closed to other.example.com
If you are just using sockets you will be doing this yourself to begin with.
Related
What is a good tactic to implement mechanism on server to read data from external device by http with keep-alive connection ?
Details.
I have camera device hosted on public address. i can use request to create connection beetween server and device ( get events in live ).
Sample request
GET /live/events HTTP/1.1
Host: x.x.x.x
Connection: keep-alive
Cookie: sid=id
and now i should get response if camera detect something.
I dont sure how i should do this. HttpClient will be enough to resolve my problem , or maybe something else ?
Regards
I am using HttpClient to make a post request. I get back 405 method not allowed. When capturing a trace in fiddler, it goes out as GET instead of POST!
using (var client = new HttpClient())
{
var url = AppSettingsUtil.GetString("url");
var response = client.PostAsJsonAsync(url, transaction).Result;
}
I am aware of the async/await issues. This is a simplified sample to show the issue.
Is there some sort of web.config or machine.config setting that could be affecting this? Other requests (sent through RestSharp) send Posts correctly
Here is what fiddler captures. Rerunning the trace in fiddler also returns the 405 (as expected). Manually switching it to POST and running works from fiddler.
Also, perhaps because the method was switched to GET, there is no body captured in fiddler, I had to manually paste in the JSON
GET /*URL*/ HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: /*host*/
Connection: Keep-Alive
The problem appears to be that someone changed the URL without telling us, and they put a redirect in place. HttpClient is responding to the redirect, but ends up actually sending the request to the final destination as a Get.
This seems like a bug in HttpClient to me, that it should either send the ultimate request as a Post, or throw an exception saying it can't do what I asked it to.
See Forwarding a response from another server using JAX-RS
I wrote an asynchronous server routine that listens for HTTP POST requests and extracts the data.
It works but the client that sends the requests is complaining about not getting the "200 OK" response when I'm done with the connection.
So I need to know which Socket method(s) returns the acknowledgement. I haven't found such info in the MSDN documentation.
List of Socket methods:
http://msdn.microsoft.com/en-us/library/vstudio/system.net.sockets.socket_methods%28v=vs.110%29.aspx
I was looking to learn more about web servers and so I followed this tutorial on codeguru to build a sample one. However, whenever I run it and load the default page, I get the standard GET http request and then another socket connection is accepted and then a blank http request is shown. The console output is below:
Web Server now listening on port 7070, press ^C to stop...
Socket Type: Stream
Client Connected!
=================
Client IP: 127.0.0.1:56310
Message received: "GET / HTTP/1.1
Host: localhost:7070
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
"
Directory requested: /
File Requested: C:\_WebServer\default.html
No. of bytes sent: 103
Total Bytes: 191
No. of bytes sent: 191
Socket Type: Stream
Client Connected!
=================
Client IP: 127.0.0.1:56311
Message received: " "
Only GET is supported :(
What is this blank http request and why is there a second socket connection even though I only made one request from my browser? Is this a sort of keep-alive thing?
Edit: Could this maybe be a timeout thing?
Edit2:
I think a reason for this might be the following:
server socket receives 2 http requests when I send from chrome and receives one when I send from firefox
It seems like Chrome does a bit of a "placeholder" socket request to optimise later transfers. I get this behaviour in IE as well though, so maybe IE is now doing something similar.
That code has so many errors that I would not recommend anyone reading it. The most severe is that it expects everything to be sent and received with a single operation which is just true for the most trivial operations.
If you want to learn how to code a web server I suggest that you look at my code: https://github.com/jgauffin/Griffin.Framework/tree/master/src/Griffin.Framework/Griffin.Core/Net/Protocols/Http
It's released under the apache license and I've separated the HTTP protocol parsing from the network IO which hopefully makes it easier to understand than the article that you linked to.
I am trying to make Http connections made by C# HttpClient helper class reusable. I read that there is KeepAlive header which has to be present in Http request.
I sniffed the traffic and I cant find any reference to KeepAlive.
It has GET and it has HTTP/1.1 strings in there. No KeepAlive no nothing.
P.S. I also tried to find KeepAlive property somewhere on HttpClient but couldnt...
Any ideas?
It should have a Connection header, which looks like this:
Connection: Close
Connection: Keep-Alive
The keep-alive header looks like this:
Keep-Alive: 115