HttpClient通过GET和POST获取网页内容
发布时间:2020-05-31 06:47:37 所属栏目:Java 来源:互联网
导读:HttpClient通过GET和POST获取网页内容
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 最简单的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 [emailprotected]
* @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();
}
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
