http Post über Servlet

Anime-Otaku

Erfahrenes Mitglied
Hallo tutorials.de Gemeinde,

mein Problem sieht wie folgt aus:

Ich brauche in meinem Servlet ein Post Request zu einem anderen Servlet. Danach soll ich die Antwort selbst weiterverarbeiten.
Beim request soll ich noch den Content-type ändern.

Vielleicht wurde diese Frage ja schon beantwortet, ich habe jedoch nichts hilfreiches gefunden.

Vielen Dank schonmal


Update:
Habs nun selbst gelöst durch den httpclient lib von apache:
http://jakarta.apache.org/commons/httpclient/apidocs/

Code:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpRecoverableException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;

public static synchronized String doHttpRequest(String requestBody,String completeURL,String ip,int port,String proxyIP,int proxyPort,Logger log)  {
		final String method = "doHTTPRequest: ";

		PostMethod postMethod = null;
		HttpConnection connection = null;
		try {
			URI uri = new URI(completeURL);
			String schema = uri.getScheme();
			if (schema == null || schema.equals("")) {
				schema = "http";
			}
			log.debug(method + "schema: " + schema);

			log.debug(method + "host : port = " + ip + " : " + port);

			connection = new HttpConnection(ip, port, Protocol.getProtocol(schema));
			if (proxyIP != null && !proxyIP.equals("127.0.0.1")) {
				connection.setProxyHost(proxyIP);
				connection.setProxyPort(proxyPort);
				log.debug(method + "proxy host : port = " + connection.getProxyHost() + " : "
						+ connection.getProxyPort());
			}
			log.info(method + "requestBody: " + requestBody);

			// attention: Get or Post
			postMethod = new PostMethod(uri.toString());
			postMethod.setRequestHeader("Content-Type","application/octet-stream");
			// set request
			postMethod.setRequestBody(requestBody);
			postMethod.execute(new HttpState(), connection);
			// InputStream result = postMethod.getResponseBodyAsStream();
			String result = postMethod.getResponseBodyAsString();
			log.info(method + "Response:" + result.toString());

			if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
				log.debug(method + "method.getStatusCode() == HttpStatus.SC_OK");
			} else {
				log.warn(method + "Unexpected failure: " + postMethod.getStatusLine());
			}

			return result.toString();

		} catch (HttpRecoverableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.close();
			}
			if (postMethod != null) {
				postMethod.releaseConnection();
			}
		}
		return null;
	}
 
Zuletzt bearbeitet:
Zurück