HttpClient通過GET和POST獲取網頁內容
最簡單的HTTP客戶端,用來演示通過GET或者POST方式訪問某個頁面
/**
- 中國銀行支付網關---銀行回調的接口
- @svncode <a href="svn://10.210.71.10/sinapay_bank/src/java/cn/com/sina">svn://10.210.71.10/sinapay_bank/src/java/cn/com/sina
- @package cn.com.sina.pay.Bank.BOC
- @author yuchao1@staff.sina.com.cn
- @date 20101014
- @access limited by password
- @reference cn.com.sina.pay.ICBC
- / import java.io.; import java.util.*; import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.xml.sax.InputSource;
import org.apache.commons.httpclient.; import org.apache.commons.httpclient.methods.; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; /**
- 最簡單的HTTP客戶端,用來演示通過GET或者POST方式訪問某個頁面
@author yuchao */ public class HttpClient1{ public static void main(String[] args) throws IOException {
String merchantNo = "104110053004253"; String orderNo = "101023416806"; String signData = "SDJFALSF"; HttpClient client = new HttpClient(); //使用POST方法 PostMethod postMethod = new PostMethod("<a href="https://ebspay.boc.cn/PGWPortal/QueryOrder.do">https://ebspay.boc.cn/PGWPortal/QueryOrder.do"); /** * 使用POST方式提交數據 */ NameValuePair[] orderInfo = {new NameValuePair("merchantNo",merchantNo),new NameValuePair("orderNos",orderNo), new NameValuePair("signData",signData),}; postMethod.setRequestBody(orderInfo); client.executeMethod(postMethod); int code = postMethod.getStatusCode(); if (code == HttpStatus.SC_OK){ String info = null; info = new String(postMethod.getResponseBodyAsString()); } /** * 打印服務器返回的狀態 */ System.out.println("the post return value"+postMethod.getStatusLine()); /** * 打印結果頁面 */ String response = new String(postMethod.getResponseBodyAsString().getBytes("UTF-8")); /** * 打印返回的信息 */ System.out.println("the getBytes() xml is:"+response); //打印返回的信息 String resCode = postMethod.getResponseBodyAsString(); System.out.println("the is my other xml:"+resCode); //釋放連接 postMethod.releaseConnection();
StringReader read = new StringReader(resCode);
InputSource source = new InputSource(read);
SAXBuilder sb = new SAXBuilder();
try{
Document doc = sb.build(source);
Element root = doc.getRootElement();
System.out.println("the getName is:"+root.getName());
List jiedian = root.getChildren();
//獲得XML中的命名空間(XML中未定義可不寫)
Namespace ns = root.getNamespace();
Element et = null;
List orderList = null;
for (int i=0;i<jiedian.size();i++)
{
et = (Element)jiedian.get(i);
if(et.getName().equals("header")){
System.out.println(et.getChild("merchantNo", ns).getText());
System.out.println(et.getChild("exception", ns).getText());
}
if(et.getName().equals("body")){
orderList = et.getChildren();
System.out.println(et.getChild("orderTrans", ns).getChild("orderStatus").getText());
}
}
for (int i=0;i<orderList.size();i++)
{
et = (Element)orderList.get(i);
if(et.getName().equals("orderTrans")){
System.out.println(et.getChild("payTime", ns).getText());
}
}
}catch(JDOMException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
</pre>
本文由用戶 n2fy 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!