Step 1: Call your doPost method within your service method.
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
doPost(req,resp);
}
Step 2: Pull your XML out of POST object, do some cleanup, pass to your relevant objects to your sendrequestgetresponse method. In my case, I'm cleaning the XML and retrieving the HOST URL, passing them both through my method.
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String strKey = " String strValue = req.getParameter(strKey);
String strConcat = strKey + "=" + strValue;
if (!WSUtils.isNullOrEmpty(strConcat)){
sendRequestGetResponse(upsHostUrl, strConcat, req, resp);
}
}
Step 3: Send your request, get your response and put it into output stream back to client.
public boolean sendRequestGetResponse(String url, String xml, HttpServletRequest req, HttpServletResponse resp)
{
//Create a method instance.
PostMethod post = new PostMethod(url);
try
{
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// set various config specs
client.getParams().setConnectionManagerTimeout(30 * 10000);
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
List
authPrefs.add(AuthPolicy.NTLM);
//provide auth policy
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
// set proxy specs
client.getState().setProxyCredentials(
new AuthScope(null, proxyPort, null),
new NTCredentials(proxyUserName, proxyPassword, "", proxyDomain));
//build request body
StringBuilder postbody = new StringBuilder(2000);
//define header
Header contentTypeHeader = new Header("Content-type", "application/x-www-form-urlencoded");
//append header object to request body
post.setRequestHeader(contentTypeHeader);
//append xml string to stringbuilder object
postbody.append(xml);
StringRequestEntity sre = new StringRequestEntity(postbody.toString(),null,null);
// append string request entity(xml) to request body
post.setRequestEntity(sre);
// execute the method
int statusCode = client.executeMethod(post);
System.out.println(statusCode);
// Read the response body - For Debugging purposes only
//byte[] responseBody = post.getResponseBody();
// Deal with the response - For Debugging purposes only
//System.out.println(new String(responseBody));
//handle response by putting into output stream back to form
InputStream is = post.getResponseBodyAsStream();
OutputStream os = resp.getOutputStream();
IOUtils.copy(is,os);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
if (statusCode == HttpStatus.SC_OK)
{
return true;
}
else
{
System.err.println("Method failed: " + post.getStatusLine());
return false;
}
}catch(HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
return false;
}catch(IOException e){
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
return false;
} finally {
// Release the connection.
post.releaseConnection();
}
}
/**
*verify URL existence
*@param urlString
*@return boolean indicating success/failure for existence
*/
public boolean verifyURL(String url)
{
try
{
if (!WSUtils.isNullOrEmpty(url))
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
}
}