使用HttpClient进行Post通信

---------------siwuxie095

   

   

   

   

   

   

首先到 Apache官网 下载相关的库文件

   

Apache官网:http://www.apache.org/

   

   

在官网页面的最下方是 Apache 所支持的所有项目列表(APACHE PROJECT LIST)

   

找到 HttpComponents,点击进入,选择 Download,下载

HttpClient 4.5.3(GA) 版本(截止 2017/3/31 最新 GA 版本)

   

页面链接:http://hc.apache.org/downloads.cgi

   

使用HttpClient进行Post通信

   

   

   

   

httpcomponents-client-4.5.3-bin.zip 解压后一览:

   

使用HttpClient进行Post通信

   

使用HttpClient进行Post通信

   

   

   

   

   

工程名:TestHttpClient

包名:com.siwuxie095.httpclient

类名:TestPost.java

   

   

   

打开资源管理器,在工程 TestHttpClient 文件夹下,创建一个

文件夹:lib,在其中放入:

1)httpclient-4.5.3.jar

(2)httpcore-4.4.6.jar

3)commons-logging-1.2.jar

   

   

工程结构目录一览:

   

使用HttpClient进行Post通信

   

   

   

全选这 3 jar 包(利用 Shift 全选),右键->Build Path->Add to Build Path

   

此时,工程结构目录一览:

   

使用HttpClient进行Post通信

   

   

   

   

代码:

   

package com.siwuxie095.httpclient;

   

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

   

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

   

   

public class TestPost {

   

public static void main(String[] args) {

   

//运行线程:Post,使用匿名对象

new Post().start();

}

   

}

   

   

class Post extends Thread{

 

//创建一个HttpClient,使用静态方法调用类HttpClientsreateDefault()

//获取一个新的实例

HttpClient client=HttpClients.createDefault();

 

//复写run()

@Override

public void run() {

 

//准备一个网络读写的接口:

//(来自于有道翻译的在线翻译接口,让其翻译:welcome

//(1)http://fanyi.youdao.com/openapi.do

//(2)keyfrom=siwuxie095-test&key=2140200403&type=data

// &doctype=xml&version=1.1&q=welcome

 

//创建一个HttpPost,传入String:指定请求的URL

HttpPost post=new HttpPost("http://fanyi.youdao.com/openapi.do");

 

 

try {

 

//执行post之前,添加发送的数据

//通过Post方式向服务器发送参数:

//keyfrom=siwuxie095-test&key=2140200403&type=data

//&doctype=xml&version=1.1&q=welcome

//

//通过setEntity()方法添加发送的数据,

//传入一个新的Entitynew UrlEncodedFormEntity() 匿名对象

//UrlEncodedFormEntity()传入一个List对象并指定字符集:UTF-8

//则需要创建一个List,类型为 BasicNameValuePair,实例化为ArrayList

//List添加参数 add(),再传入新的BasicNameValuePair->键值对

List<BasicNameValuePair> list=new ArrayList<>();

list.add(new BasicNameValuePair("keyfrom", "siwuxie095-test"));

list.add(new BasicNameValuePair("key", "2140200403"));

list.add(new BasicNameValuePair("type", "data"));

list.add(new BasicNameValuePair("doctype", "xml"));

list.add(new BasicNameValuePair("version", "1.1"));

list.add(new BasicNameValuePair("q", "welcome"));

post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));

 

 

//HttpClient执行post操作

//execute()返回HttpResponse类型,创建以接收返回值

//有异常抛出,使用 try catch 捕获

HttpResponse response=client.execute(post);

 

 

//response中获取当前请求的结果

//getEntity()返回HttpEntity类型,创建以接收返回值

HttpEntity entity=response.getEntity();

 

 

//entity返回成一个String类型

//调用类EntityUtils的静态方法toString()

//传入entity的同时,指定转换为字符串时使用的编码

String result=EntityUtils.toString(entity, "UTF-8");

 

//打印结果,输出为 HTML 数据

System.out.println(result);

 

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

 

 

}

 

 

}

   

   

运行一览:

   

使用HttpClient进行Post通信

   

   

   

   

   

关于 有道翻译 API,详见本人博客的分类:来一杯Java

里面的 使用Get进行Http通信使用Post进行Http通信

   

   

本人博客(任选一个)链接:

https://www.baidu.com/s?ie=UTF-8&wd=siwuxie095

   

   

   

   

   

【made by siwuxie095】