httpClient学习笔记1
客服端以post请求输入xml的输入流,来到服务器端,服务器端接到输入流,进行处理,处理完毕后,返回xml信息的返回输出流,来告诉对方成功与否。
htppClient的使用至少需要commons-httpclient-3.1.jar,commons-logging-1.0.4.jar,commons-codec-1.3.jar三个Apache开源项目jar包的支持。(jar的版本可以不同,我用的是如上三个。)
模拟客户端代码:
package httpClientDemo;
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
public class HttpClientTest {
private static final String LOGON_SITE = "localhost" ;
private static final int LOGON_PORT = 8080;
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
File input = new File("d:\\test.xml");
PostMethod post = new PostMethod("/Mytest/servlet/abc.do");
NameValuePair name = new NameValuePair( "name" , "zhangjinping" );
NameValuePair pass = new NameValuePair( "password" , "123456" );
post.setRequestBody( new NameValuePair[]{name,pass});
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=utf-8");
// post.setRequestHeader( "Content-type" , "text/xml; charset=utf-8" );
post.setRequestEntity(entity);
try {
int result = client.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}
/* // 设置请求的内容直接从文件中读取
post.setRequestBody( new FileInputStream(input));
if (input.length() < Integer.MAX_VALUE)
post.setRequestContentLength(input.length());
else
post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
// 指定请求内容的类型
int result =client.executeMethod(post);
System.out.println( "Response status code: " + result);
System.out.println( "Response body: " );
System.out.println(post.getRequestCharSet());
System.out.println(post.getResponseBodyAsString());
post.releaseConnection();
} */
}
}
服务器端代码:
package web;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Student;
import bo.CreateBD;
import common.CreateXMLUtil;
public class AjaxTestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public AjaxTestServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.");
String str = request.getParameter("testPost");
String name= request.getParameter("name");
String password = request.getParameter("password");
System.out.println(name+" "+password);
@SuppressWarnings("unused")
StringBuffer sb = new StringBuffer();
InputStream is= request.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while(true){
str = br.readLine();
if(str!=null)
sb.append(str);
if(str==null)
break;
}
System.out.println(sb.toString());
response.setContentType("application/xml"); //application/xml代表的是XML形式返回
response.setHeader("Cache-Control", "no-cache"); //设置不缓存
List<Student> students = CreateBD.getData();
//组织返回数据
String xml=CreateXMLUtil.getClassXML(students, "students");
PrintWriter pw=null;
try {
//获取页面写入器
pw=response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
pw.write(xml);
pw.flush();
pw.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
Apache官方:httpClient 详解:
http://hc.apache.org/httpclient-3.x/
Apache官方:httpClient使用xmlPOST的举例代码:http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/
转载于:https://www.cnblogs.com/alaricblog/p/3278275.html
httpClient学习笔记1相关推荐
- HttpClient 学习整理
HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的实现,发现这个开源项目之后就有点眉目了,令人头痛的cookie问题还是有办法解决滴.在网上整理了一些东西,写得很好,寄放在这里 ...
- HttpClient 学习整理(转)
来自 http://www.blogjava.net/Alpha/archive/2007/01/22/95216.html HttpClient 是我最近想研究的东西,以前想过的一些应用没能有很好的 ...
- Android学习笔记---27_网络通信之通过GET和POST方式提交参数给web应用,以及使用httpClient,来给web项目以post方式发送参数
Android学习笔记---27_网络通信之通过GET和POST方式提交参数给web应用,以及使用httpClient,来给web项目以post方式发送参数 27_网络通信之通过GET和POST方式提 ...
- 《Angular4从入门到实战》学习笔记
<Angular4从入门到实战>学习笔记 腾讯课堂:米斯特吴 视频讲座 二〇一九年二月十三日星期三14时14分 What Is Angular?(简介) 前端最流行的主流JavaScrip ...
- ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
http://blog.jobbole.com/85008/ ASP.Net MVC开发基础学习笔记(5):区域.模板页与WebAPI初步 2015/03/17 · IT技术 · .Net, Asp. ...
- ASP.NET Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记
任务10:第三方ClientCredential模式调用 创建一个控制台程序 dotnet new console --name ThirdPartyDemo 添加 Nuget 包:IdentityM ...
- tornado学习笔记day08-tornado中的异步
概述 应为epoll主要用来解决网络的并发问题,所以tornado中的异步也是主要体现在网络的IO异步上,即异步web请求 tornado.httpclient.AsyncHTTPClient tor ...
- 8. SpringBoot基础学习笔记
SpringBoot基础学习笔记 课程前置知识说明 1 SpringBoot基础篇 1.1 快速上手SpringBoot SpringBoot入门程序制作 1.2 SpringBoot简介 1.2.1 ...
- Java基础学习笔记(二)_Java核心技术(进阶)
本篇文章的学习资源来自Java学习视频教程:Java核心技术(进阶)_华东师范大学_中国大学MOOC(慕课) 本篇文章的学习笔记即是对Java核心技术课程的总结,也是对自己学习的总结 文章目录 Jav ...
最新文章
- 自动sqlldr脚本
- P1133 教主的花园
- 在Windows下编译WebRTC
- 转【FullPage.js 应用参数参考与简单调用】
- C# FolderBrowserDialog 的用法
- vue.js 多页 php,如何使用 vue-cli 开发多页应用方法
- 住宅按套内面积算,医院人脸识别黄牛,DNA碱基对可能会扩充,菜鸟发布供应链系统,猪瘟不影响食品安全,这就是今天的大新闻...
- #1123-JSP隐含对象
- tastypie使用cache对list data无效问题
- linux 配置用户密码,Linux ——用户密码相关设置
- Oracle PL/SQL 程序设计读书笔记 - 第13章 其他数据类型
- Maximum call stack size exceeded 如何解决?
- 计算机文化基础清华大学PPT,数据库基础知识清华大学计算机文化基础.ppt
- 十个相见恨晚的编程工具
- matlab神经网络训练方法,matlab神经网络模型导出
- matlab 画根轨迹,4.4 绘制根轨迹的MATLAB函数 | 学步园
- 从gitlab上down下来的项目Django页面加载不出来
- 实现闲鱼自动化脚本-方案对比分析
- UVALive 6678 Judging Troubles
- 学霸,顾名思义,就是成绩非常好
热门文章
- 很不错的Windows 控件 Developer Express Inc.NET
- @Java | Thread synchronized - [ 线程同步锁 基本使用]
- 关于微信小程序swiper的问题
- shiro身份验证测试
- python简单爬虫(一)
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- 新人报道,写的东西还请大神们多指导!也希望能让和我一样的同事少走弯路。...
- 【C++基础】时间类型详解(转)
- 配置nginx-rtmp流媒体服务器(宝塔面板配置教程)
- armeabi和armeabi-v7a的区别