Java socket with http multipart formdata POST method - java

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.

Related

Java library for async 'multipart-form' file upload?

I am trying to upload a file (and additional fields) using single REST POST request. This request has to be processed asynchronous and form has to be submitted using multipart-form content type (because beside file I am sending few other text fields with certain metadata).
I tried with Apache's org.apache.http.impl.nio.client.CloseableHttpAsyncClient but until now I didn't succeed (when request arrives on the server, fields from the form are not presented). When I do the same using org.apache.commons.httpclient.HttpClient request is processed without problems, so the issue is not on the server side.
Does anybody know how this async 'multipart-form' request could be made? If not possible with CloseableHttpAsyncClient, do you have some other library to suggest?
Thanks in advance...
I just want to give an update. I did some investigation and was able to fulfill my requirements with AsyncHttpClient (AHC)
It works very fast and is based on Netty Framework and NIO Java API.

Java - Sending complete HTTP message (including headers, status line aso)

For some reasons I need to develope an own HTTP client. I managed working with the HTTP protocol, but I don't know how to send it via java...
For example, I got the following request message I want to send (I couldn't test it, so I am not sure that it is in a correct format):
GET http://example.com HTTP/1.1
Transfer-Encoding:UTF-8
someOtherParam=thatIsThis&name=value
All I could find was making up a connection using URL.openConnection() - but with that connection, I can't send the whole message, but have to put the header values via the connection.addRequestProperty() method.
Can anyone help me and tell me how to send such a message to a server?
(And yes; I do know libraries like Apache HttpComponents :))
Best greetings,
Martin Bories
thanks for your help - Sockets did it :).
For anyone who is running into the same problem: Use sockets - you can simply write and receive messages and implement an own HTTP implementation.

Send additional information over HTTP header

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).

How do I use Apache Http Components to relay a POST request from a servlet?

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.

Java HttpServletResponse with blank-value headers?

How can I make a java-based application server reply with an empty-valued response header, like this?
content-length:\r\n
Unfortunately when I call
response.setHeader("Content-Length", len)
where len is either an empty string or null, the response will not include the header.
I've checked the HttpServletResponse and HttpServletResponseWrapper javadocs but couldn't figure out what could be overriden to provide my custom behaviour.
Background
I'm building a testing application that is supposed to emulate badly-behaved HTTP server scenarios. The application is supposed to reply to requests with preset pages and HTTP headers, including malformed ones like the above case.
The application is written in grails.
I'm building a testing application that is supposed to emulate badly-behaved HTTP server scenarios.
In such a case, attempting to get a well-behaving server to mimic such behavior is a bad idea. If you need to mimic a bad server, or a particular set of scenarios you wish to test, then you may do one of the following:
write a custom application that listens on a particular port (using the ServerSocket class) that will respond with malformed HTTP headers. Using HTTP libraries may not help, for libraries may have code to detect erroneous conditions and correct them automatically.
use a HTTP proxy that is capable of intercepting responses and allows for modifications of these responses. You will find several if you Google for "http debugging proxy", but if you haven't heard of any, I would suggest looking at Fiddler, WebScarab or Burp.
You can try a tool like SoapUI or Fiddler with it's Firefox extension. I havent tried setting a malformed header with them but I wouldn't be suprised if you could.
Something not clear for me: your application is written in Grails, but you are discussing of javadocs... Well, I suppose you try to create a bad server in JAVA...
As you said, answering with "Content-Length:\r\n" is not legal for HTTP. You must put an integer value or discard the header. I think setHeader() helps you to avoid to produce an illegal HTTP message.
You can workaround this way creating manually the headers (you can write directly to the socket without using the setHeader blocks).
Other solution is to create a filter (in addition of your servlet) with your own implementation of HttpServletResponse. You will pass this implementation to the servlet.

Categories