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方法调 ...
随机推荐
- Upgrading to Java 8——第三章 Optional and Similar Classes
Java程序员对付空指针异常已经好多年了.在Java8中将有新的方式去处理他们.通过包装一个潜在的可能为null的类称为Optianal. 在Java8中添加了the Optional, Option ...
- STL容器的适用情况
转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...
- 设计模式之组合模式(Composite)
组合模式原理:组合模式的作用是讲继承同一父类的不同子类对象组合起来,形成一个树形的结构,例如公司的部门组织 代码如下 #include <iostream> #include <st ...
- Python大数据依赖包安装
一.安装 先安装python2.7.6,win下的numpy这些包需要直接匹配版本,然后安装“numpy-1.8.1-win32-superpack-python2.7”和“scipy-0.16.0- ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- AngularJs学习笔记--Guide教程系列文章索引
在很久很久以前,一位前辈向我推荐AngularJs.但当时我没有好好学习,仅仅是讲文档浏览了一次.后来觉醒了……于是下定决心好好理解这系列的文档,并意译出来(英文水平不足……不能说是翻译,有些实在是看 ...
- css hack一览
浏览器对css hack的支持情况
- HDOJ 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Sql调用WebService
DECLARE @scid int,@rt int ) --创建MSSOAP.SoapClient组件(如果安装的是SoapToolkit30,应该是MSSOAP.SoapClient30,否则是MS ...