package com.jerry.httpclient;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; /**
*
* @author Jerry
* @date 2015年2月11日 下午11:44:46
*/
public class ParameterDemo {
public static void main(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/services/query");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("word", "女神"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
package com.jerry.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*
* @author Jerry
* @date 2015年2月11日 下午11:31:16
*/
public class DictionaryServlet extends HttpServlet{ /**
*
*/
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String chineseWord = req.getParameter("word");
String englishWord = translate(chineseWord);
out.write(englishWord);
} private String translate(String chineseWord) {
// TODO Auto-generated method stub
String result = "";
if ("女神".equals(chineseWord)) {
result = "goddness";
} else if ("屌丝".equals(chineseWord)) {
result = "loser";
} else {
result = "查无此单词";
}
return result;
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}

[HttpClient]传递参数的更多相关文章

  1. HTTP的DELETE方法Body传递参数问题解决

    理论上,Delete method是通过url传递参数的,如果使用body传递参数呢? 前提: 使用HttpClient发送http请求 问题: httpDelete对象木有setEntity方法 解 ...

  2. Feign发送Get请求时,采用POJO对象传递参数的最终解决方案 Request method 'POST' not supported (附带其余好几个坑)

    yml: feign: httpclient: enabled: true properties: #feign feign.httpclient.enabled=true <!-- https ...

  3. feignclient发送get请求,传递参数为对象

    feignclient发送get请求,传递参数为对象.此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中. 第一步: 修改application.yml中配置feign发送请求使用apache ...

  4. Vue 给子组件传递参数

    Vue 给子组件传递参数 首先看个例子吧 原文 html <div class="container" id="app"> <div clas ...

  5. [转] C++的引用传递、指针传递参数在java中的相应处理方法

    原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...

  6. 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)

    目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...

  7. 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数

    1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...

  8. Linux线程体传递参数的方法详解

    传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...

  9. 【hadoop】如何向map和reduce脚本传递参数,加载文件和目录

    本文主要讲解三个问题:       1 使用Java编写MapReduce程序时,如何向map.reduce函数传递参数.       2 使用Streaming编写MapReduce程序(C/C++ ...

随机推荐

  1. bzoj 2463 [中山市选2009]谁能赢呢?(博弈)

    2463: [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1290  Solved: 944[Submit][Stat ...

  2. 全国各城市Uber客服联系方式(电话、邮箱、微博)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. microsoft visual studio 不能逐句执行?

    最近在使用vs2013过程中,vs2013不能逐行执行,在不设断点的情况下,根本停不下来按一下F11,整个程序就执行完了.百度了很久也没找到方法(看的还不够仔细),卸载安装了很多次(看我够笨的吧),最 ...

  4. windows 下文件上传到fastdfs

    php.ini 配置 [fastdfs]; the base pathfastdfs_client.base_path = D:/tmp ; connect timeout in seconds; d ...

  5. RabbitMQ 概念

    RabbitMQ快速概念入门 转(http://blog.csdn.net/qq_16414307/article/details/50585630) 本文适有一定消息队列基础的,但没有接触过Ra ...

  6. uva11549 Floyd判圈法

    题意: 给两个数n, k,每次将k平方取k的前n位,问所有出现过的数的最大值 原来这就是floyd判圈法.. #include<cstdio> #include<cstdlib> ...

  7. android 加载图片防止内存溢出

    图片资源: private int fore[]; private int back[]; fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawa ...

  8. html5之canvas困惑 在canvas标签内需要设置了宽跟高,如果在css中设置同样的宽跟高,画出来的图像变形了?

    <canvas class="cvs"></canvas> 遇到的问题: 如css 中设.cvs{width:500px;height:400px;},也就 ...

  9. 下载的chm手册打不开的解决方法?

    用ie或者chrome等浏览器下载文件的时候,都会给文件加上一个默认的保护,右键这个文件,打开属性对话框,然后在第一个的选项卡的安全的部分,有个解除这个保护的按钮点下然后确定保存,再打开chm文件就不 ...

  10. cocos2d与cocos2d-X中的draw和update

    像其它的游戏引擎一样,我们有两个不同的方法来完成draw和update. 1: Draw:每一个CCNode都有一个draw方法,每一帧都会调用.我们只在这个方法里做描绘的事情. 2: Update: ...