举例:
java.lang.StringBuilder sbReq = new java.lang.StringBuilder();
sbReq.append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
sbReq.append("<RequestData>");
sbReq.append("<clickSysId>%s</clickSysId>");
sbReq.append("<userAccount>%s</userAccount>");
sbReq.append("<retCode>%s</retCode>");
sbReq.append("<mKey>%s</mKey>");
sbReq.append("<timestamp>%s</timestamp>");
sbReq.append("</RequestData>");
String sReqXml = sbReq.toString();
sReqXml = String.format(sReqXml, clickSysId, userAccount, retCode ,smKey, stimestamp);
StringBuilder就是拼String,拼好了还可以用format格式化里面的参数。
post方式post xml,并获取返回状态:
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://*****/***.aspx");
try{
method.setRequestHeader("Content-Type","text/xml; charset=GB2312");
method.setRequestBody(sReqXml);
client.executeMethod(method);
InputStream resStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
StringBuffer resBuffer = new StringBuffer();
String resTemp = "";
while((resTemp = br.readLine()) != null){
resBuffer.append(resTemp);
}
sResult = resBuffer.toString();
System.out.println("sResult:" + sResult);
//截取字串。。。。
//
}catch(Exception e){
e.printStackTrace();
}finally{
method.releaseConnection();
}