HttpClient示例

 1 <%@page import="com.sun.xml.ws.client.BindingProviderProperties"%>
 2 <%@page contentType="text/html;charset=UTF-8" language="java" %>
 3 <%@page import="java.util.*"%>
 4 <%@page import="java.io.*"%>
 5 <%@page import="org.apache.commons.httpclient.*"%>
 6 <%@page import="org.apache.commons.httpclient.methods.*"%>
 7 <%
 8 request.setCharacterEncoding("UTF-8");
 9 String url = request.getParameter("url")==null?"":request.getParameter("url");
10 String message = request.getParameter("message")==null?"":request.getParameter("message");
11 String result = "";
12 
13 try {
14     //call service
15     if(url!=null && !"".equals(url)) {
16         PostMethod postMethod = new PostMethod(url);
17           byte[] bytes = message.getBytes("UTF-8");
18           InputStream inputStream = new ByteArrayInputStream(bytes, 0,
19                   bytes.length);
20           RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
21                   bytes.length, "application/soap+xml; charset=utf-8");
22           postMethod.setRequestEntity(requestEntity);
23           //设置header
24             postMethod.setRequestHeader("OperationCode","com.mss.jt.cf.interfaces.SI_CF_YCHD_BALANCE_OUT_SynService");
25             postMethod.setRequestHeader("ClientId","com.mss.js.cf");
26             postMethod.setRequestHeader("TransactionId","B6B7B99D928B8581F92C4CBAEF3075F31508DB7F0BF856736A8AEBC25ADE69A0");
27             // 用户名密码:zpiappluser:zpiappl
28           postMethod.setRequestHeader("Authorization", " Basic "
29                 + (new sun.misc.BASE64Encoder()).encode("zpiappluser:zpiappl"
30                         .getBytes()));
31           HttpClient httpClient = new HttpClient();
32           
33           int statusCode = httpClient.executeMethod(postMethod);
34           bytes = postMethod.getResponseBody();
35           result = new String(bytes,"UTF-8");
36     }
37 }catch(Exception e) {
38     e.printStackTrace();
39     result= "异常:"+e.getMessage();
40 }
41 %>
42 <html>
43 <head>
44 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
45 <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
46 <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
47 <META HTTP-EQUIV="Expires" CONTENT="0">
48 </head>
49 <body>
50 <form id="form1" name="form1" method="POST" action="./test3.jsp">
51 url:<input type="text" name="url" style="600px" value="<%=(url!=null && !"".equals(url))?url:"http://132.228.26.15:29200/jt/wl/SDMS_ESB_INTEGRATION?wsdl"%>" ><p>
52 message:<textArea name="message"  cols="100" rows="5"><%=message%></textArea>
53 <p>
54 result:<textArea name="result" cols="100" rows="5"><%=result%></textArea>
55 <p>
56 <button type="button" onclick="javascript:return dosubmit();" >提交</button>
57 </form>
58 </body>
59 <script type="text/javascript">
60 <!--
61 function dosubmit() {
62     if(document.form1.url.value=="") {
63         alert("请输入url!");
64         document.form1.url.focus();
65         return;
66     }
67     document.form1.submit();
68 }
69 //-->
70 </script>
71 </html>
原文地址:https://www.cnblogs.com/ding0910/p/3547656.html