今天需要post请求并且得到php返回的xml数据
这里我利用dom4j,因为返回的数据我需要组成xml
public class DanjiudianchakanTag extends OrderBaseTag{
@Override
public Document dataToXml() {
String html="";
String st=request.getParameter("st");
String et=request.getParameter("et");
String PropertyID=request.getParameter("PropertyID");
String str="http://192.168.10.218/search/singleHotel.php?wc=000&hotelname="+PropertyID+"&indate="+st+"&outdate="+et;
System.out.println(str);
Document doc = Dom4jHelp.CreateDocument();
response.setContentType("text/html;charset=utf-8");
//利用HttpClient进行请求
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(str);
getMethod.addRequestHeader("Content","text/html,charset=utf-8");
try{
int statusCode = httpClient.executeMethod(getMethod);
if(statusCode!=HttpStatus.SC_OK){
System.err.println("Method failed: "+ getMethod.getStatusLine());
}
// 读取内容
byte[] responseBody = getMethod.getResponseBody();
// 处理内容
html = new String(responseBody);
//返回的信息
System.out.println(html);
SAXReader saxReader = new SAXReader();
//转化为doc对象
doc = saxReader.read(new ByteArrayInputStream(html.getBytes()));
}
catch(Exception e){
System.err.println("页面无法访问");
}
getMethod.releaseConnection();
try {
MessageToXML.SystemWriter(doc);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
}
接下来:
利用Httpclient进行post 提交数据。
首先建立Myhttpclient
public class MyHttpClient {
public String requestPHP(String url,String wc,String order)throws IOException {
HttpClient client = new HttpClient();
//服务器地址
client.getHostConfiguration().setHost("192.168.10.218", 80, "http");
//post 参数
HttpMethod method = getPostMethod(url,wc,order);//使用POST方式提交数据
method.addRequestHeader("Content","text/html,charset=gb2312");
// HttpMethod method = getGetMethod(url,wc,order);//使用get方式提交数据
client.executeMethod(method);
System.out.println("----------"+method.getStatusLine());
//打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("gb2312"));
//打印返回的信息
System.out.println("---------"+response);
method.releaseConnection();
return response;
}
/** *//**
* 使用GET方式提交数据
*
*/
private static HttpMethod getGetMethod(){
return new GetMethod("/search/hotelorder.php");
}
private static HttpMethod getGetMethod(String url,String wc,String order){
return new GetMethod("/search/hotelorder.php?wc=000&order="+order);
}
/** *//**
* 使用POST方式提交数据
* @return
*/
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod("/search/hotelorder.php");
NameValuePair simcard = new NameValuePair("simcard","1330227");
post.setRequestBody(new NameValuePair[] { simcard});
return post;
}
private static HttpMethod getPostMethod(String url,String wc,String order){
PostMethod post = new PostMethod(url);
NameValuePair pwc = new NameValuePair("wc",wc);
NameValuePair porder = new NameValuePair("order",order);
post.setRequestBody(new NameValuePair[] {pwc,porder});
return post;
}
}
如何调用:
String url = "/search/hotelorder.php";
String wc = "000";
MyHttpClient mc = new MyHttpClient();
try {
String rphp = mc.requestPHP(url,wc,order);
doc = DocumentHelper.parseText(rphp);
Element r = doc.getRootElement();
Dom4jHelp.getNextItem(r,"Internet",Internet);//宽带
Dom4jHelp.getNextItem(r,"BedType",BedType);//床型
} catch (Exception e1) {
e1.printStackTrace();
}
这样就可以通过上MyHttpClient进行post提交。
主要jar:commons-httpclient-3.1.jar
分享到:
相关推荐
php中http请求封装HttpClient精华中的经典代码。从国外官网直接下的,看看人家怎么封装的,受益匪浅啊,堪称经典。里边有说明和实例
HttpClient.php类文件,用于模拟get或post请求,源码中基本使用例子
HttpClient 通过HTTP/HTTPS协议获取数据,如要fork请点击github连接过去,本库只是同步库,谢谢!
require_once ( 'httpclient.php' ); $ http = new HttpClient ( "http://mydomain.com" ); $ http -> get ( "/info.php" ); echo $ http -> getBody (); // Just print the response body 也可以这样写: <?...
Unirest 是一个轻量级的 HTTP 请求库,可发起 GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 请求。支持 Node、Ruby、Java、PHP、Python、Objective-C、.NET 等多种语言。底层是基于httpclient,所以使用Unirest...
|-----------req.php (支付请求文件,通过此文件发起支付请求,商家可以在此文件中写入自己的订单信息等,然后把请求提交给易宝支付) |-----------callback.php(支付结果返回文件,通过此文件商家判断对应订单的支付...
GET请求 $ http = new HttpClient ();$ http -> set_header ( 'User-Agent' , 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1' ); $ http -> get ( 'http://www.example.com/' ); echo $ ...
小米帐号开放平台OAuth PHP SDK使用说明小米OAuth简介小米帐号开放平台文档PHP SDK说明php-sdk/httpclient/XMApiClient.php -- 基础Http请求封装php-sdk/httpclient/XMOAuthClient.php -- 针对OAuth授权流程相关http...
一个HttpClient组件,可以发送HTTP POST、GET请求。 用PHP面向对象的思想实现
LogBundle 为所有ETSGlobal Symfony应用程序提供标准化的日志... 提供一个Symfony HttpClient装饰器,以通过HTTP调用转发令牌。 安装 安装捆绑包 composer require etsglobal/log-bundle 加载捆绑 // config/Bundles.
有一个解决方法(如下): /* Somewhere in config/session.php */'lifetime' => 15,但这也会影响 CSRF 令牌,我们不希望那样。 这就是 Laravel 身份验证超时的用武之地。 Laravel Auth Timeout 是一个小型中间件包...
HttpClient组件HttpClient组件提供了强大的方法来同步或异步获取HTTP资源。 该组件是实验性的。 Symfony的向后兼容HttpClient组件未涵盖实验性功能。...资源文档贡献报告问题,并在主Symfony存储库中发送拉取请求
Unirest是一个轻量级的HTTP请求库,涵盖Node、Ruby、Java、PHP、Python、Objective-C、.NET等多种语言。可发起GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS请求。 特性 1.能够伪造GET, POST, PUT, PATCH, DELETE,...
本文实例讲述了PHP封装请求类。分享给大家供大家参考,具体如下: 1.源码 <?php namespace app\common\components; use Yii; use app\common\services\BaseService; class HttpClient extends BaseService{ ...
用于PSR-7的HttpClient中间件 使用PSR-7消息的HTTP客户端中间件缺少接口。 目的是提供您可以在应用程序和非http库中依赖的接口。 这样,我们可以实现独立的代码,每个人都可以使用他想要的任何HTTP客户端。 可能有...
Codeigniter 基本 HTTP 客户端使用 PHP cURL 简化 GET/POST 请求,目前仅支持 JSON 格式。 XML,图像功能即将推出。安装将HttpClient.php放入您的应用程序/库中用法初始化 $ this -> load -> library ( 'HttpClient'...
|------------------------------HttpClient.class.php (共通函数文件,用于服务器通讯) 2)商家测试可以先用易宝支付的测试商家测试成功再在merchantProperties.php文件中修改成自己的商家ID和密钥信息 $p1_MerId =...
Guzzle是一个PHP HTTP客户端,可以轻松发送HTTP请求并轻松与Web服务集成。 简单的界面,用于构建查询字符串,POST请求,流式传输大型上传文件,流式传输大型下载文件,使用HTTP cookie,上传JSON数据等。 可以...
Unirest 支持多种语言,如Node、Ruby、Java、PHP、Python、Objective-C、.NET 等,可发起 GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 请求 作为基于apache httpcomponent的unirest-java,提供了更为便捷、功能...