1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85
|
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SOAPClientServlet extends HttpServlet
{
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String requestURI = req.getRequestURI();
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println ("<html>");
pw.println (" <head><title>SOAPClientServlet</title></head>");
pw.println (" <body>");
pw.println (" <b><center>");
pw.println (" <form method='post' action='"+requestURI+"'>");
pw.println (" <p>SOAP URL: <input name='url' type='text' size='64'>");
pw.println (" <p>SOAP Action: <input name='soap-action' type='text' size='64'>");
pw.println (" <p>SOAP Envelope:<p><textarea name='input' cols='50' rows='15'></textarea>");
pw.println (" <p><input type='submit' value='Submit'>");
pw.println (" <p><input type='reset' value='Reset'>");
pw.println (" </form>");
pw.println (" </body>");
pw.println ("</html>");
pw.flush();
pw.close();
} // end doGet
public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
String url = req.getParameter ("url");
String soapAction = req.getParameter ("soap-action");
String input = req.getParameter ("input");
URL u = new URL (url);
HttpURLConnection uc = (HttpURLConnection)u.openConnection();
uc.setDoOutput (true);
uc.setDoInput (true);
uc.setRequestProperty ("Content-Type", "text/xml; charset=utf-8");
uc.setRequestProperty ("Content-Length", String.valueOf(input.length()));
uc.setRequestProperty ("SOAPAction", soapAction);
uc.setRequestMethod ("POST");
PrintWriter pw = new PrintWriter (uc.getOutputStream());
pw.println (input);
pw.close();
res.setContentType("text/html");
PrintWriter pw2 = res.getWriter();
BufferedReader br = new BufferedReader (new InputStreamReader(uc.getInputStream()));
String line = null;
while ((line=br.readLine())!=null)
{
pw2.println (escapeHtml(line));
}
br.close();
pw2.close();
} // end doPost
private String escapeHtml (String str)
{
StringBuffer sb = new StringBuffer();
int j = str.length();
for (int i=0;i<j; i++)
{
char c = str.charAt(i);
switch (c)
{
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
default:
sb.append(c);
break;
}
}
return sb.toString();
} // end escapeHtml
} // end class SOAPClientServlet
|