I have written a simple HTTP server using Java and want to send some additional information (puzzle parameters and a small puzzle solver program) to the client i.e. a regular browser.
Similarly, the browser is also supposed to send information (solution) back to the server.
Is there a way to do this by just transmitting this information over the HTTP headers?
Thanks a lot
the headers are usually used to add http protocol relevant information.
You should probably use either the body of the response or cookies to add the needed information.
Adding a cookie is done using the header so it kind of fits what you are asking for.
But I wonder why you need to put it in the header? it seems like what you are asking for is url parameters (client to server) and response body (server to client).
Related
I'm using java and I'm looking for solution to get respond from website.
The problem is i need to create a handshake with server for about 30 sec and then send header and body to finish request.This also a multipart/formdata POST method.
can anyone suggest me where to find tutorial or some example of code?
Thank you.
You can surely work lower-level (TCP-level) blocking-style as in http://examples.javacodegeeks.com/core-java/net/socket/send-http-post-request-with-socket/
I'm confident there should be a way to do that in Netty, you could try having a look at How to send a request with POST parameters in Netty?.
I couldn't find other Java libraries that would allow you to wait before sending headers, but many (including JDK's URLConnection) will allow you to pre-configure headers and then stream the body.
I'm currently programming for Android 4.4 and I'm wanting to fill a web form with a username and password combination. This is obviously sensitive data, and I'm connecting to a HTTPS web page.
Is using the HTTP POST method sensible and encouraged for this or is there an alternate method I can use to do it?
HTTP Post vs HTTP GET
in GET method you send the parameters part of the url string
in POST method you send the parameters part of the post body
HTTP vs HTTPS
in HTTP you send the request as plain text, very readable by 3rd party applications
in HTTPS you encrypt the contents of the request, and thus the 3rd party applications would need some additional keys to decrypt it.
the question of sensitivity depends on who do you want to hide the information from?
if from the user, HTTP POST is enough, the average user does not try to read the contents of http requests.
if from hackers, then you'd need HTTPS
I need to send a request from a servlet to an application running in other environment (IIS) with certain information in custom headers.
I know redirecting doesn't send the headers and getServletContext().getRequestDispatcher(url) is to be used in the servlet's context only. Has anybody made this work in some other way? I was thinking in using HttpURLConnection, but would it finally redirect the browser to the targeted app ?
Thanks in advance
You can't redirect from your servlet to the external server and keep the headers, and you can't forward the incoming request to other context.
What you could do, is to use the HttpURLConnection (or other http client library such as HTTPClient) to make the request (with the custom headers you need) to the remote server and, once the operation is complete, redirect the user to the external site (customm headers are not set in this redirection).
This is a little tricky, if you elaborate your question (what do you really need to do) we can probably think about other alternatives.
AJAX? You can send some JavaScript code to the browser which sends a request to the ISS and handle (shows) its answer. It's possible to set http headers with XMLHttpRequest but it needs client side JavaScript coding and you have to find a way to send cross domain requests.
I'm a little unfamiliar both with the Servlet API and Apache Http Components.
I need to handle an incoming POST request with unknown data (although probably the result of a form submission) using HttpServlet.doPost() which I've implemented, and request the same posted information from another URL, effectively acting as a relay for the HTTP POST. I then need to convert the response to a String (it will be text/html) and process it further before returning it to the web browser that requested it from me.
Due to my unfamiliarity with these libraries, its not clear to me how to handle issues like the content-type of the posted data, and also avoiding any problems due to neglecting to release resources.
Can anyone provide any pointers on this?
You should start by having a look at HttpClient class from apache API.
It will handle both get and posts as needed and later you could feel its request with the data you receive in your own servlet.
Is it possible to use java socket API to read content of a webpage, ex: "www.yahoo.com"? Can somebody here show an example?
And how about reading content of a page protected by the web app login screen?
Thanks in advance,
dara kok
It's possible but not advisable. The webpage is returned using HTTP, which is more than just a stream of bytes. This means that in order to use a socket you application would need to understand the instructions in the HTTP responses and behave accordingly.
To programitically access a webpage use Jakarta Commons HTTP Client.
With regards to secure webpages, it will depend on how they are secured, however given HTTP Client can maintain cookies you should be able to perform the login through code too.
Further to Nick's answer (i.e. use the Jakarta commons HTTP Client). The login security depends on how the login page is implemented, if it is an apache .htaccess secured site you will need to place username/password information in the request header. Alternatively (and generally more usual), if it is an html form, you will need to deconstruct the form fields from the original HTML and send those as key/value parameters in the http GET/POST request