Question

The query I have has something do with an implementation I`m doing.

When a HTTP request is sent from the client Port to the Server Port, I understand that the response is also sent back to the same port(Port to Port communication).

In my case, the server is forwarding the response to a URL with Query String to a host server on our network. So what I get when reading the response using the InputStream of the URLConnection object is the HTML content corresponding to the Login page of the forwarded URL with no query string.

I suspect this is because the URL is modified by our host server.

Now the question is, in this scenario there seems to be an intermediate entity which is the our host server to which URL is forwarded. So, when I read the response(URL forwarded by server) back in the InputStream, I`m not sure which of the following 2 is the actual scenario happening:

1.) Response is directly read from the external Server(as this is a Port to Port communication)

OR

2.)Response received from the intermediate Host Server which sees to be the case.

(If the 2nd scenario is correct, would the intermediate host server know which client to correctly forward the response to?)

URL url = new URL(httpsURL);
HttpsURLConnection urlConnection = (HttpsURLConnection) url
                .openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true); 
///
Omitting code for SSL
///
String urlParameters = "CCNumber=4111111111111111";
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

BufferedReader in = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream()));
String inputLine;
if (urlConnection.getResponseCode() == HttpsURLConnection.HTTP_OK){

 while ((inputLine = in.readLine()) != null) {
  System.out.println(inputLine);
 }
}
Was it helpful?

Solution

You receive the response from the intermediate server.

It sounds like your intermediate server acts as a (reverse) proxy server. When you make a request to your intermediate server, it in turn makes a request to the server on your network (just as you described it). However, the "real" server has usually no idea that the request was initiated by you. In particular, it does not know your IP address or the port you sent your request from. The only thing it does know is the IP address and port of the intermediate (the proxy) server. The proxy server on the other hand still knows the IP address and port from which you sent your request.

So you send your request to the proxy, the proxy sends it to the "real" server, the real server sends its response back to the proxy, which sends it back to you. You and the "real" server do not communicate directly with each other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top