// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = //new HttpPost("http://crmetl.yishenghuo.com:8142/outserver/startUpByHand.htm");
new HttpPost("http://localhost:8087/outserver/startUpByHand.htm");
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("userName", "admin"));
formparams.add(new BasicNameValuePair("passWord", "123456"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity((List<? extends org.apache.http.NameValuePair>) formparams, "UTF-8");
httppost.setEntity(uefEntity);
CloseableHttpResponse httpResponse = httpclient.execute(httppost);
try {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
//0:更新成功,1:更新失败,2:更新中,3:距离上次更新不足1个小时
JSONObject json = new JSONObject();
if("0".equals(entity)){
json.put("code", "0");
json.put("message", "更新成功!");
}else if("1".equals(entity)){
json.put("code", "1");
json.put("message", entity+"更新失败!");
}else if("2".equals(entity)){
json.put("code", "2");
json.put("message", entity+"更新中!");
}else if("3".equals(entity)){
json.put("code", "3");
json.put("message", entity+"距离上次更新不足1个小时,请稍候再更新。");
}
json.put("message", "未知错误。。。");
response.getWriter().print(json.toJSONString());
logger.info("更新结果:"+EntityUtils.toString(entity, "UTF-8"));

}
} finally {
httpResponse.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}

httpclient模拟服务器请求的更多相关文章

  1. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  2. HttpClientUtil [使用apache httpclient模拟http请求]

    基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ...

  3. 使用httpClient模拟http请求

    在很多场景下都需要用到java代码来发送http请求:如和短信后台接口的数据发送,发送数据到微信后台接口中: 这里以apache下的httpClient类来模拟http请求:以get和Post请求为例 ...

  4. 关于HttpClient模拟浏览器请求的參数乱码问题解决方式

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/44407297 http://www.llwjy.com/blogdetail/9 ...

  5. HttpClient模拟http请求

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...

  6. HttpClient模拟客户端请求实例

    HttpClient Get请求: /// <summary>        /// Get请求模拟        /// </summary>        /// < ...

  7. httpclient模拟post请求json封装表单数据

    好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. public static String httpPostWithJSON(String url) throws Exception ...

  8. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

  9. java模拟http请求

    java模拟http发送请求,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main.utils; impo ...

随机推荐

  1. javascript实现快速排序算法

    忘记了快速排序的思路是怎样的了,复习一下,写了两个实例,发表博文备忘. 对于快速排序的思想,可以参考白话经典算法系列之六 快速排序 快速搞定,讲得比较通俗 prototype扩展的方式 /** * 对 ...

  2. VUE搭建脚手架CLI

    1.vue的安装基于node,一定要确保本机已经安装node,node安装完成之后,会自带npm包.node下载地址:nodejs.cn/download/ 安装完成以后使用 ,进入cmd使用以下命令 ...

  3. jquery 使用a标签导航栏跳转页面,动态添加高亮

    众所周知,使用a标签跳转之后,会刷新一次,继而这个添加的样式就会消失.那么怎么解决这一问题呢? <script> $(function () { $('.bar a').each(func ...

  4. python 安装 pip ,并使用pip 安装 filetype

    闲话少说,直接上操作. python版本为2.7.6 可以直接到官网下载,我也提供一个百度云的下载地址 https://pan.baidu.com/s/1kWPXG8Z 这个是window版本,lin ...

  5. PCB项目 X1 STC12C5A60S2-LQPF48

    单片机控制系统双层板STC51 简介: STC12C5A60S2主芯片,12MHz主频 12V电源输入,12/5/3V电源输出 4路0~12V可调10位ADC输入 4路1A大电流达林顿输出 4路INT ...

  6. CSS3 flexbox弹性布局实例

    常用例子 1.居中对齐 <!DOCTYPE html> <head> <meta charset="utf-8"> <style type ...

  7. Python---基础---常用的内置模块

    一. print(math.pow(4,3))   # 4的三次方 #幂运算    函数返回浮点型,幂运算返回整形 print(4**3) #fabs()   对一个数值获取他的绝对值    返回的也 ...

  8. JavaWeb(九):上传和下载

    表单 进行文件上传时, 表单需要做的准备: 1). 请求方式为 POST: <form action="uploadServlet" method="post&qu ...

  9. c++11 默认函数的控制

    1. 类与默认函数: C++中声明自定义的类,编译器会默认生成未定义的成员函数: 构造函数 拷贝构造函数 拷贝赋值函数(operator=) 移动构造函数 移动拷贝函数 析构函数 编译器还会提供全局默 ...

  10. jquery用法初探

    JQuery选择器 JQuery选择器用于查找满足条件的元素,比如可以用$(“#控件Id”)来根据控件id获得控件的jQuery对象,相当于getElementById: 1.id 选择器   $(“ ...