I’m trying to make a Java server that can accept and return data using POST. So far I have been trying to use nameValuePairs (although another method be fine) and have the server accepting connections, and accepting the HTTP header, but from here I cannot fathom how to get any attached data. The following code prints out the header and sends a 100 response once the client expects it – but how do I get the data from the message?
while(true){
inputLine = clientInput.readLine();
System.out.println("client-msg: " + inputLine);
if("Expect:".equals(inputLine.split(" ")[0])){
sendResponse(100, "Continue");
break;
}
Here is what the (android java) client is sending, I’m looking to get the name pair out on the server side
ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("field", "data here"));
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(
postParams);
request.setEntity(uefe);
HttpResponse response = client.execute(request);
This is probably fairly simple but I couldn’t seem to find any non PHP tutorials for POST.
If you really need to do this by hand you should read the HTTP RFC because it is potentially even more complicated than what you are trying to do now. If what you really want is a way to embed a small Java web server into something check out Jetty. It is really easy to embed Jetty into other apps.
It should be as easy as: