HttpClient通过GET和POST获取网页内容
最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面
/**
* 中国银行支付网关---银行回调的接口
* @svncode 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("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();
}
}
}
HttpClient通过GET和POST获取网页内容的更多相关文章
- 基于HttpClient、Jsoup的爬虫获取指定网页内容
不断尝试,发现越来越多有趣的东西,刚刚接触Jsoup感觉比正则表达式用起来方便,但也有局限只适用HTML的解析. 不能尝试运用到四则运算中(工作室刚开始联系的小程序). 在原来写的HttpClient ...
- 基于apache —HttpClient的小爬虫获取网页内容
今天(17-03-31)忙了一下午研究webmagic,发现自己还太年轻,对于这样难度的框架(类库) 还是难以接受,还是从基础开始吧,因为相对基础的东西教程相多一些,于是乎我找了apache其下的 H ...
- 使用Jsoup获取网页内容超时设置
使用Jsoup获取网页内容超时设置 最近使用Jsoup来抓取网页,并对网页进行解析,发现很好用.在抓取过程中遇到一个问题,有些页面总是报Timeout异常,开始想是不是被抓取网站对IP进行了限制,后来 ...
- 【C#】获取网页内容及HTML解析器HtmlAgilityPack的使用
最近经常需要下载一些东西,而这个下载地址又会经过层层跳转,每个页面上都有很多广告,烦不胜烦,所以做了一个一键获得最终下载地址的小工具.使用C#,来获取网页内容,然后通过HtmlAgilityPack获 ...
- C#获取网页内容的三种方式
C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse... 方法一:使用WebClient (引用自:http: ...
- C#获取网页内容 (WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
获取网页数据有很多种方式.在这里主要讲述通过WebClient.WebBrowser和HttpWebRequest/HttpWebResponse三种方式获取网页内容. 这里获取的是包括网页的所有信息 ...
- 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)
定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) import urllib.request def get_ ...
- C#获取网页内容的三种方式(转)
搜索网络,发现C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse... 方法一:使用WebClient (引用 ...
- 使用selenium和phantomJS浏览器获取网页内容的小演示
# 使用selenium和phantomJS浏览器获取网页内容的小演示 # 导入包 from selenium import webdriver # 使用selenium库里的webdriver方法调 ...
随机推荐
- 在Eclipse新建菜单中添加JSP
在开发的时候,大家可能选择不同的透视图,下面以Java EE透视图为例. 在项目上右键,选择new命令,出来的菜单中并没有新建JSP的选项. 这样一来,如果想新建JSP,只能选择Other命令,在里面 ...
- Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM)
02 MAY, 2012 The new Ubuntu release has just rolled around and with it a slew of new packages. Perso ...
- 开始学习C++ Templates
设计模式的学习以及各种设计模式的C++实现已经告一段落了,解释器模式和迭代器模式,以及模版办法简单的看了一下. 画UML图的时候再重点学习 从今天开始学习Templates.
- Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏
转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式: A. 是按值传递的? B. ...
- Lessons learned from manually classifying CIFAR-10
Lessons learned from manually classifying CIFAR-10 Apr 27, 2011 CIFAR-10 Note, this post is from 201 ...
- nginx 多站点配置方法集合(转)
关于nginx的多站设置,其实和apache很相似,假设我们已经有两个域名,分别是:www.websuitA.com和www.websuitB.com.并且这两个域名已经映射给了IP为192.168. ...
- OpenLayers3 online build
openlayers3使用了一个比较复杂的build工具,从github上下载下来的代码中并没有build之后的版本,要配置build环境又比较繁琐,好在官方的example中提供了在线的版本,下面就 ...
- docker-py的配置与使用
测试环境 75机:Red Hat Enterprise Linux Server 7.0,无外网访问权限 73机:Red Hat Enterprise Linux Server 7.0,无外网访问权限 ...
- DevOps:怎么实现源代码注释和系统文档的自动化更新?
[编者按]计算机软件传统定义为:软件是计算机系统中与硬件相依存的另一部分,软件包括程序.数据及其相关文档的完整集合.然而在时下的开发中,文档的合规性往往被忽视的干干净净.本文由 Todd Waits ...
- 转 wince程序 中使用Listview显示图标问题 (C#) .
思路: 1.窗体控件:lstaqgl [Listview控件名称] imageList1[ImageList控件] 2. 图片路径添加到—imageList1——Listview显示图片从 ima ...