Access website by ipaddress - c#

I'm trying to access websites via the ip address rather than http address. I am trying out a number of well know sites such as microsoft and google and getting their ip address by pinging them. For instance 184.87.106.199 is microsoft and 216.58.221.68 is google.
async Task<HttpStatusCode> RequestPage(string url, HttpClient client) {
var request = new HttpRequestMessage();
try {
var response = await client.GetAsync("http://" + url);
Console.WriteLine(string.Format("{0} - {1}", url, response.StatusCode.ToString()));
return response.StatusCode;
} catch (TaskCanceledException) {
Console.WriteLine(string.Format("{0} - Timeout", url));
return HttpStatusCode.GatewayTimeout;
}
}
However it does not appear to work for every site. google works fine if i request http://216.58.221.68 but microsoft returns a bad request status.
what am i missing?

You cannot reliably visit a website based on the IP address. Often a server is configured to host multiple websites on a single IP address, and based on the URL it will serve the correct website.

When you make an HTTP request to a website with your browser, the browser itself will do the lookup for IP address and then connect. However, your browser will send something like this:
GET http://stackoverflow.com/ HTTP/1.1
Host: stackoverflow.com
This means the remote end knows which website to send you to as the server could be hosting many sites, particularly in a shared hosting environment.
Also, to complicate matters even further, some websites will have multiple IP address.

Related

How to return ftp response from ftp proxy, when request incoming to proxy is over http?

At start thank you all for your kind help! Every hint is great and can teach me a lot, I appreciate every comment! Before you respond I'm aware that there is a lot of working solutions and working ftp proxies, but I'm asking this question for learning purposes and I try to understand how it works.
I have to implement my own ftp proxy server to capture ftp commands. The issue is that request incoming to proxy from client is over http protocol and when I return raw ftp response from ftp server through proxy to the client, then client browser is not able to handle this response as ftp response. Currently I'm trying to hardcode initial response from ftp proxy as following:
var clientWriter = new BinaryWriter(clientNetStream);
clientWriter.Write("220 (vsFTPd 3.0.3)\r\n");
But when client browser got this response, then it is showing following box:
https://imgur.com/XcIy7Rw
Because above does not work then I've tried to include ftp response in HttpResponse as follows:
var clientWriter = new BinaryWriter(clientNetStream);
clientWriter.Write(
"HTTP/1.1 200 OK\r\n" +
"Date: Mon, 19 Jul 2004 16:18:20 GMT\r\n" +
"Server: Apache\r\n" +
"Last-Modified: Sat, 10 Jul 2004 17:29:19 GMT\r\n" +
"Accept-Ranges: bytes\r\n" +
"Content-Length: 9328\r\n" +
"Connection: keep-alive\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"220 (vsFTPd 3.0.3))\r\n");
But then browser handle this as follows:
https://imgur.com/JuFTjs7
What I try do to is return "220 (vsFTPd 3.0.3)\r\n" response from server to the client and then I expect that client will send "USER anonymous\r\n" command to log in, but with both solutions this is not happening. I try to make working following sequence automatically (please see Wireshark screenshot). https://imgur.com/yT3dRxW
Does anybody knows how to return response from server to the client, to make client to communicate with ftp server?
If client send http request to the proxy then can I return Ftp response instead of http response? If not, then how proxy response should looks like?
It is important to distinguish between a HTTP Proxy and a FTP proxy.
A HTTP proxy is typically capably to handle URLs starting with http(s):// and ftp://.
For URLs starting with ftp://, the proxy does a protocol transformation.
That means that the client uses the HTTP protocol to transmit the URL to the proxy (in this case starting with ftp://). Due to the URL starts with ftp://, the proxy knows that it must use the FTP protocol to connect to the server. This is referred to as FTP over HTTP as the client is connecting to a FTP server using a HTTP client.
Alternatively, the client can use a native FTP client to connect to the server using the proxy. In this case, the client uses the FTP protocol to talk to the proxy and the proxy uses the FTP protocol to talk to the server. This is referred to as native FTP proxy.
As you already noticed, in the FTPoverHTTP scenario, the client does not follow up on any FTP response. Instead, you have to implement the protocol transformation in the proxy accordingly. Pass the username and password from client to proxy as follows: ftp://username:password#server.com. Your proxy must extract the credentials from the URL, connect to the server, and then issue the USER and PASS command on behalf of the client.
Hint: The proxy also needs to extract directory/filenames from the URL in order to get a directory listing / file content using the FTP protocol from the server, then present it to the client accordingly, e.g. directory listing as html page with links to the artifacts in the directory or in case a file has been accessed, send a response which triggers a HTTP file download on the client

How can a c# client receive http POST?

I am currently working on a simple app to make my life easier. I made an android app which lets me pick a file and uploads it to a server. I am working on a windows PC c# app that sends its ip (dynamic) and its open port to my server.
Whenever the server receives a file from my phone, I want it to send a POST request to my PC.
I am fairly new to web stuff (I have done tons of coding before though), but as far as I understand only a server can receive a POST request.
How can I make a C# server that runs on my PC with a dynamic IP and receives POST requests?
I have been struggling with this for a while now, just simple keywords I should research would be very helpful, thanks.
HTTP is a protocol which lets Web Servers and Clients communicate. It requires you to have a web server (IIS, Apache or other) to respond to client http requests.
Client can send a GET, POST and others request type messages.
The prefable way is to send a web client using a WebClient class. Here is a sample taken from another answer given by Andrew
string URI = "site.com/mail.php";
using (WebClient client = new WebClient())
{
System.Collections.Specialized.NameValueCollection postData =
new System.Collections.Specialized.NameValueCollection()
{
{ "to", emailTo },
{ "subject", currentSubject },
{ "body", currentBody }
};
string pagesource = Encoding.UTF8.GetString(client.UploadValues(URI, postData));
}
I would create some kind of server application using WebAPI, SignalIR, WCF or ASMX web services. All of these can handle server/client communication and would make it easy to communicate with your device.

Request.Url.GetLeftPart(UriPartial.Authority) returns http on https site

We use Request.Url.GetLeftPart(UriPartial.Authority) to get the domain part of the site. This served our requirement on http.
We recently change site to https (about 3 days ago) but this still returns with http://..
Urls were all changed to https and show in browser address bar.
Any idea why this happens?
The following example works fine and returns a string with "https":
var uri = new Uri("https://www.google.com/?q=102njgn24gk24ng2k");
var authority = uri.GetLeftPart(UriPartial.Authority);
// authority => "https://www.google.com"
You either have an issue with the HttpContext class right here, or all your requests are still using http:
You can check the requests HttpContext.Current.Request.IsSecureConnection property. If it is true, and the GetLeftPart method still returns http for you, I think you won't get around a replacing here.
If all your requests are really coming with http, you might enforce a secure connection in IIS.
You should also inspect the incoming URL and log it somewhere for debugging purposes.
This can also happen when dealing with a load balancer. In one situation I worked on, any https requests were converted into http by the load balancer. It still says https in the browser address bar, but internally it's a http request, so the server-side call you are making to GetLeftPart() returns http.
If your request is coming from ARR with SSL Offloading,
Request.Url.GetLeftPart(UriPartial.Authority) just get http

Test Client's ability to connect via https - ASP.net

I have an ASP.Net page that when the user arrives, I would like to test if they are able to connect to another page on my server via HTTPS connection. If TLS is not enabled in the user's settings, they are being refused access.
If the test fails, then I would like to display a specific message.
I have considered using:
WebClient _client = new WebClient();
and
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myurl....");
but these are performing the requests from the server side and therefore they connect without issue even if the client can't connect themselves.
I have also considered an ajax request; however, I cannot make an https request from http because of the Same Origin Policy
Do you all have any ideas that would allow me to test https while on an http page?
Thanks in advance
You can create an <img> tag pointing to a valid image in the HTTPS page, then handle its load and error events.

How do I get the request IP address on a SSL request

I have a service that is running in Amazon Ec2. The service exposes both a http endpoint and a https endpoint. I am doing some geo lookup on the user IP address when I log the data. Everything works just fine on requests coming into the http endpoint. I have to grab the X-Forwarded-For header so that I do not take the Amazon Load Balancer UP Address and I am always able to get what I need. However on requests that come in on the https endpoint all of the IP addresses are the same.
In order to pull the IP address I am using the following C# code:
public static string FetchClientIp(HttpRequest req)
{
var value = req.Headers["X-Forwarded-For"];
return string.IsNullOrEmpty(value) ? req.UserHostAddress : value;
}
I can't find anything else that I need to do that is specific to https requests so I'm hoping someone here has run into this before. I'm going to spin up a test on this to try to better isolate the problem.
Thanks
It depends how you have your ELB set up.
If you're terminating SSL on the ELB (new feature as of October 2010), then the client IP address will be in "X-Forwarded-For".
HttpContext.Current.Request.Headers["X-Forwarded-For"]
It sounds like you're terminating SSL on your web servers, then ELB can't decrypt the traffic and add the "X-Forwarded-For" header to the HTTP request. So the client IP address in the header "REMOTE_ADDR" (which is the header returned by HttpRequest.UserHostAddress) is the IP of the last hop -- in this case the internal IP address of the ELB.
Keep in mind, "X-Forwared-For" may contain multiple IP addresses as described at http://docs.amazonwebservices.com/ElasticLoadBalancing/latest/DeveloperGuide/index.html?SvcIntro.html#X-Forwarded-For. In that case, you're probably most interested in the first address listed.

Categories