/**
* Sends a message to the given web host and site and receives its respond
* @param pHost the host name
* @param pSite The site
* @param pMessage The message string
* @param pTimeout the connection timeout (sec)
* @return the response from the web server
* @throws Exception if the message could not send to the host
*/
public static Map<String, String> sendHttpPost(String pHost, int pPort, String pSite, String pMessage, int pTimeout, boolean pDebug) throws Exception {
System.out.println("sendHttpPost() host = " + pHost + ", port = " + pPort + ", site = " + pSite + ", message = " + pMessage);
Socket socket = null;
OutputStream out = null;
byte[] theMessage = null;
try {
theMessage = pMessage.getBytes("UTF-8");
StringBuffer msgBuffer = new StringBuffer();
msgBuffer.append("POST");
msgBuffer.append(" " + pSite + " ");
msgBuffer.append("HTTP/1.1");
msgBuffer.append("\r\n");
msgBuffer.append("Host: ");
msgBuffer.append(pHost);
msgBuffer.append("\r\n");
msgBuffer.append("Content-type: ");
msgBuffer.append("application/x-www-form-urlencoded");
msgBuffer.append("\r\n");
msgBuffer.append("Content-length: ");
msgBuffer.append(String.valueOf(theMessage.length));
msgBuffer.append("\r\n");
msgBuffer.append("\r\n");
msgBuffer.append(pMessage);
if (pDebug) {
System.out.println("==========================================");
System.out.println("AHttpPost.sendHttpPost() send = " + msgBuffer.toString());
}
byte[] message = msgBuffer.toString().getBytes("UTF-8");
// write datas to the host 'pHost' on pPort
if (pTimeout == 0) {
pTimeout = CONNECTION_TIMEOUT;
}
socket = new Socket(pHost, pPort);
socket.setSoTimeout(1000 * pTimeout); //set socket timeout (ms)
out = socket.getOutputStream();
out.write(message);
out.flush();
//
// read answer if any
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(inputStreamReader);
Map<String, String> response = new HashMap<String, String>();
String line = null;
boolean ok = false;
while ((line = br.readLine()) != null) {
if (pDebug)
System.out.println("AHttpPost.sendHttpPost() response = " + line);
if (line.indexOf(RESPONSE_OK_ID) >= 0) {
ok = true;
break;
}
//split the response into a key/value pair
String[] keyValue = line.split("=");
if (keyValue.length == 2) {
response.put(keyValue[0], keyValue[1]);
}
}
if (ok) {
return response;
} else {
String errMsg = " Internal server error " + pHost;
//show the last line from the server
if (line != null) {
errMsg += "\n" + line;
}
throw new Exception(errMsg);
}
} catch(UnknownHostException uhex) {
throw new Exception("Keine Verbindung zu " + uhex.getMessage());
} catch(ConnectException cex) {
throw new Exception("Keine Verbindung zu " + cex.getMessage());
} catch(SocketTimeoutException tex) {
throw new Exception(tex.getMessage());
} catch (IOException ioex) {
throw ioex;
} catch (Exception ex) {
throw ex;
} finally {
try {
socket.close();
out.close();
} catch (Exception ioex) {}
}
}