public HttpResponse doHttpCall(final String urlString,
			final String stringToSend, final String user, final String pwd,
			final String proxyHost, final int proxyPort,
			final Map<String, String> httpHeaders) throws IOException {

		HttpURLConnection connection = null;
		HttpResponse result = new HttpResponse();
		try {
			URL url = new URL(urlString);

			if (!StringUtils.isEmpty(proxyHost)) {
				Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
						proxyHost, proxyPort));
				connection = (HttpURLConnection) url.openConnection(proxy);
			} else {
				connection = (HttpURLConnection) url.openConnection();
			}

			connection.setRequestProperty("Content-Length",
					String.valueOf(stringToSend.length()));
			connection.setRequestProperty("Content-Type", "text/xml");
			connection.setRequestProperty("Connection", "Close");
			connection.setRequestProperty("SoapAction", "");
			connection.setDoOutput(true);
			connection.setUseCaches(false);

			Set<Entry<String, String>> entrySet = httpHeaders.entrySet();
			for (Entry<String, String> key : entrySet) {
				connection.setRequestProperty(key.getKey(), key.getValue());
			}

			if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(pwd)) {
				String auth = (user + ":" + pwd);
				String encoding = new Base64Encoder().encode(auth
						.getBytes("UTF-8"));
				connection.setRequestProperty("Authorization", "Basic "
						+ encoding);
			}
			// Send request
			DataOutputStream wr = new DataOutputStream(
					connection.getOutputStream());
			wr.writeBytes(stringToSend);
			wr.flush();
			wr.close();

			// Get Response
			LOG.info("response code = " + connection.getResponseCode());
			InputStream is;

			result.setResponseCode(connection.getResponseCode());
			if (connection.getResponseCode() <= HTTP_400) {
				is = connection.getInputStream();
			} else {
				/* error from server */
				is = connection.getErrorStream();
			}

			BufferedReader rd = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			String line;
			StringBuilder response = new StringBuilder();
			while ((line = rd.readLine()) != null) {
				response.append(line);
				response.append('\r');
			}
			rd.close();

			LOG.info("response " + response.toString());
			result.setResponseContent(response.toString());

		} finally {

			if (connection != null) {
				connection.disconnect();
			}
		}
		return result;
	}
public HttpResponse doHttpCall ( final String urlString , final String stringToSend , final String user , final String pwd , final String proxyHost , final int proxyPort , final Map < String , String > httpHeaders ) throws IOException { HttpURLConnection connection = null ; HttpResponse result = new HttpResponse ( ) ; try { URL url = new URL ( urlString ) ; if ( ! StringUtils . isEmpty ( proxyHost ) ) { Proxy proxy = new Proxy ( Proxy . Type . HTTP , new InetSocketAddress ( proxyHost , proxyPort ) ) ; connection = ( HttpURLConnection ) url . openConnection ( proxy ) ; } else { connection = ( HttpURLConnection ) url . openConnection ( ) ; } connection . setRequestProperty ( "Content-Length" , String . valueOf ( stringToSend . length ( ) ) ) ; connection . setRequestProperty ( "Content-Type" , "text/xml" ) ; connection . setRequestProperty ( "Connection" , "Close" ) ; connection . setRequestProperty ( "SoapAction" , "" ) ; connection . setDoOutput ( true ) ; connection . setUseCaches ( false ) ; Set < Entry < String , String > > entrySet = httpHeaders . entrySet ( ) ; for ( Entry < String , String > key : entrySet ) { connection . setRequestProperty ( key . getKey ( ) , key . getValue ( ) ) ; } if ( ! StringUtils . isEmpty ( user ) && ! StringUtils . isEmpty ( pwd ) ) { String auth = ( user + ":" + pwd ) ; String encoding = new Base64Encoder ( ) . encode ( auth . getBytes ( "UTF-8" ) ) ; connection . setRequestProperty ( "Authorization" , "Basic " + encoding ) ; } DataOutputStream wr = new DataOutputStream ( connection . getOutputStream ( ) ) ; wr . writeBytes ( stringToSend ) ; wr . flush ( ) ; wr . close ( ) ; LOG . info ( "response code = " + connection . getResponseCode ( ) ) ; InputStream is ; result . setResponseCode ( connection . getResponseCode ( ) ) ; if ( connection . getResponseCode ( ) <= HTTP_400 ) { is = connection . getInputStream ( ) ; } else { is = connection . getErrorStream ( ) ; } BufferedReader rd = new BufferedReader ( new InputStreamReader ( is , "UTF-8" ) ) ; String line ; StringBuilder response = new StringBuilder ( ) ; while ( ( line = rd . readLine ( ) ) != null ) { response . append ( line ) ; response . append ( '\r' ) ; } rd . close ( ) ; LOG . info ( "response " + response . toString ( ) ) ; result . setResponseContent ( response . toString ( ) ) ; } finally { if ( connection != null ) { connection . disconnect ( ) ; } } return result ; }
Sends a string to a URL. This is very helpful when SOAP web services are
involved or you just want to post a XML message to a URL. If the
connection is done through a proxy server you can pass the proxy port and
host as parameters. Also, in case the connection requires basic
authentication you can set the user name and password. A map of HTTP
headers can be provided in the for of (key, value)

@param urlString
the url to post to
@param stringToSend
the string that will be send
@param user
the user name in case the connection uses basic authentication
@param pwd
the user password in case the connection uses basic
authentication
@param proxyHost
- the proxy host in case the connection will be made through a
proxy server
@param proxyPort
- the proxy port in case the connection will be made through a
proxy server
@param httpHeaders
- a list of HTTP headers to be set when posting
@return a {@link HttpResponse} object containing the response code and
the response string
@throws IOException
if a I/O error occurs