How to send HTTP request from one machine to another - java

Context:
I have never created something or worked on this subject before but I have done my research and now I know what's the subject.
I have a virtual server machine(machineVM) and not a virtual client machine (machineA).
I should send a HTTP request from machineVM to machineA to a specific port. machineVM sees and knows machineA,i.e, it knows client name of machineA(myClient) via the HTTP request .
I'd like to say to machineA: "I have a document for you, come and get it.". On machineA I will have a windows service that will listen to that port and do its stuff.
That HTTP request should look like:
http:\\myClient:port/request_message_body
What are the possible wayouts to achieve the task?
HTTP POST ?
I am aware that HTTP uses port 80. How to decide and which port to use, which port will windows service listen, on which port should HTTP request be sent?
In this request_message_body I'd like to share a path to the folder from which machineA could fetch the document and to specify server and port.
How to format HTTP request correctly?
I should be able to create windows service that listens to a port, because I've created few services before, but I really need help with creation of this HTTP request and sending it.
Any help will be appreciated.

If you want to roll-your-own solution, you'll probably want to build on top of httpclient
There's also loads of libraries out there to help you to build restful webservices (springws, resteasy, jaxrs etc)

Solved!
Thank you #Gimby and #user2412816. You guys made me go through my code again and it was my mistake, like the most of the time. :-)

Related

Is possible know who is requesting my server through GET request using Servlet?

I'm implement a control of requests in my server and i need know... Is possible know who is requesting my server through GET request without use any parameter to identify the client? If it can be possible I'll improve my development considerably.
For example: Know what's the IP, MAC Address, PC/Device name and others...
Thanks for help.
Since you are on a servlet environment, and quoting from here:
ServletRequest.getRemoteAddr(): Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.
ServletRequest.getRemoteHost(): Returns the fully qualified name of the client or the last proxy that sent the request.
There are more in the ServletRequest class and the HttpServletRequest (ref) - the class of the parameter that gets passed to the servlet service methods.
In the Servlet you can use
request.getRemoteAddr();
to get the accessing machine IP and then store it in the database.
Look at your localhost_access_log.txt in your tomcat logging directory. This is probably the best you can do without any client side code. This will show the origin IP address of the GET request though.

HTTP Client-Server resquest response

I am trying to write a simple client-server application in Java using HTTP request/response. I would like the client to be a desktop program which sends (posts) a request to a server. The server is a web page which will be hosted on Apache Tomcat server. The server must be able to read the information and display it on the browser and must be able to respond to the client with a status code 200. I am using eclipse and Apache tomcat server. I have so far tried various resources, but all I could find is a client which could request response from an already existing web server. Could someone please give me an example or some insight on how to make the client request the our own server which runs on the local machine.
Good question, though in your case, I won't recommend you implement a simple HTTP request/response approach as you will end up implementing a timer, heartbeat or Comet. You might wanna try javax or jetty WebSocket API. All you need is to create three parts:
a websocket.server
a websocket.client (desktop application)
a javascript websocket client (browser agent)
Your server and both clients will become full-duplex via onMessage and send events.
Here's an example which I believe is a bit relevant one.
https://dzone.com/articles/sample-java-web-socket-client

Faking User-Agent when sending request to server?

I am reading Detecting Device Type in a web application and just got curious if it would be possible to client to fake the User-Agent when sending the request?
Question(s)
- user sends request via curl command but fakes it to look as if request is coming from Mobile on the server? is it possible?
- Can server detect it?
- Can server prevent it?
Thanks
It is possible and easy. All you have to do is set the user-agent header string. I've seen a browser that allowed you to set it (don't remember which one). On the server it is very hard to know. A lot of bots pretend to be a browser so they don't get filtered out.

Servers sending XML packet via HTTPS to each other

I think this might be a very stupid question.
I have 2 servers that are going to communicate with each other via HTTPS using XML packets.
My question is this, there is no webpage just a Java application. How would I get my Java app to run when it hears an XML packet from the other server? Are the messages to be sent via POST?
I know about HTTP protocols but my knowledge goes as far as webservers to sending the webpage to browsers, server to server communication w/o a webpage is baffling me. Can anyone point me to the right direction or better still to some codes that I might be able to look at.
Thank you.
You can use HttpClient in your server applications so that they become clients to each other. Then maybe simply implement REST services for the 'clients' to call.
I would suggest trying this out firstly using HTTP before trying it in HTTPS.
Of course there are other technologies you could try E.g JMS or SOAP.

how to find out the url request from a socket?

Hi i am running a server socket created in java that accepts requests from a client that uses HTTP url to connect and send web requests.
client does this:
URLconnection.opeonConnection(new Url(www.hello.com/hi));
on my server socket it retrieves a Socket object.
is it possible to retrieve the url www.hello.com/hi url that was passed to this socket conection?
Probably, by parsing the client's HTTP headers:
You can get the "/hi" portion with some work. The URL Connection class will use the HTTP protocol to submit some HTTP headers, along with a GET request as "GET /hi HTTP/1.1". The "/hi" portion comes from the URL.
The "www.hello.com" part might be more difficult. The browser will provide the host and port (if port is given) portion of the URL in a "Host: " header. It's likely that your networking infrastructure may contain proxies or load balancers that forward the request on to the final backend server, and if the proxies aren't configured correctly, they won't maintain the original host header. Try it yourself, and talk to your network administrators if it's not working as you expect.
If you want to see what the URL connection class will send to your server you can get an idea by pointing your browser to this URL: http://request.urih.com/. The "request header: raw source" section shows you the HTTP headers sent by an HTTP client (your browser).
The "HTTP Headers for Dummies" web page is also a good introduction to HTTP headers
Yes it is possible to get the String but not the way you likely want it.
You can implement a HTTP-Server (see RFC 2616). But it is likely that you don't want this.
You can also use a Servlet container like Tomcat or Jetty and write a Servlet to use HTTP. With this the Servlet container does all the protocol and you can concentrate on doing the business logic.
As you state you're building a proxy, you're already asking the wrong question.
An HTTP proxy only has to deal with one command: the CONNECT command. You just have to:
Read this one line from the client.
Connect to the target indicated in the command.
Reply with success or failure.
If success, start copying bytes in both directions.
But I don't know why you're even implementing an HTTP proxy when there are dozens of free working ones already in existence.
In in the interface Socket there is getRemoteSocketAddress(). It returns a SocketAddress, and if the implementation of SocketAddress is InetSocketAddress, try to call getHostString().
SocketAddress address =yourSocket.getRemoteSocketAddress().
String address=((InetSocketAddress)address).getHostString();
Hope it helps.

Categories

Resources