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方法调 ...
随机推荐
- 通过XMLHttpRequest和jQuery实现ajax的几种方式
AJAX大家已经都知道了,是为了实现异步通讯,提高用户体验度,而将很多旧知识(XML,DOM,JavaScript,HTML,Jquery,Css……)重新融合的一个新的知识框架.而,XMLHttpR ...
- java 邮箱验证公共方法
- HTTP1.1缓存策略
以下是一幅虽然信息包含量有限.但足够以最简洁的方式说明了“什么是HTTP1.1缓存策略”的图 缓存和缓存策略 web缓存(web cache)或代理缓存(proxy cache)是一种特殊的HTTP ...
- [百度空间] --whole-archive & --no-whole-archive
What is it? backgorund: an archive file (.a) is similar as .lib compared to Winodws. it simply conta ...
- 抛弃jQuery 深入原生的JavaScript
虽然我已经做网站建设工作10多年了,但我从最近3年才开始更多地学习如何更好的将纯JavaScript用于工作中,而不总是将jQuery考虑在第一位.现在我每天学习很多东西.这个过程让我觉得Adtile ...
- Red hat Linux 安装Node.js 源码安装
1. 下载源码包 http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz 2.准备安装环境,>python2.6, gcc, g++ pytho ...
- POJ 2141
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("1 ...
- line-height 与垂直居中!
在此之前,对于line-height 与垂直居中的问题,经常碰到. 比如,图片与span在同一个box中的时候,竟然会各种偏移.要想达到理想的效果真的是各种难. 有时间,决定认真的啃一啃. 一 lin ...
- JavaScript基于对象编程
js面向对象特征介绍 javascript是一种面向(基于)对象的动态脚本语言,是一种基于对象(Object)和事件驱动(EventDirven)并具有安全性能的脚本语言.它具有面向对象语言所特有的各 ...
- redis命令参考
http://doc.redisfans.com/ 进入redis命令行模式方式: 1.进入redis安装目录 2.运行redis-cli