近期在对接项目时用到http方式与第三方交互数据,由于中间沟通不足导致走了不少弯路,至此特意花了点时间总结服务端与客户端数据交互的方式,本地搭建两个项目一个作为服务端,一个作为客户端。post可以有两种方式:一种与get一样,将请求参数拼接在url后面,这种服务端就以request.getParameter获取内容;另一种以流的方式写入到http链接中,服务端再从流中读取数据,在HttpURlConnection中分别用到了GET、POST请求方式,HttpClient以及commons-httpClient均以POST请求为例。

服务端代码:

package com.lutongnet.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Server_ccy
*/
@WebServlet("/Server_ccy")
public class Server_ccy extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Server_ccy() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
System.out.println("server-------doGet..start.");
String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println("server-------params:"+name+":"+password);
System.out.println("server-------doGet..end.");
PrintWriter out = response.getWriter();
out.print("{\"姓名\":\"陈昌圆\"}");
out.flush();
out.close();
//response.getWriter().append("server_get_info:").append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
response.setContentType("application/json;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
System.out.println("server-------doPost..start.");
System.out.println("ContentType---->"+response.getContentType());
System.out.println("queryString---->"+request.getQueryString());
String name = request.getParameter("name");
System.out.println("name---->"+name);
//String password = request.getParameter("password");
//System.out.println("server-------params:"+name+":"+password);
StringBuffer sb = new StringBuffer("");
String str;
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
if((str = br.readLine()) != null){
sb.append(str);
}
System.out.println("从客户端获取的参数:"+sb); System.out.println("server-------doPost..end.");
PrintWriter out = response.getWriter();
out.print("{\"姓名\":\"陈昌圆\"}");
out.flush();
out.close();
} }

客户端代码:

1.HttpURLConnection主要详细分析GET与POST两种请求方式,我们项目中的api就是用的这种

package com.lutongnet.HttpURLConnection;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date; public class HttpURLConnectionTest { public static void main(String[] args) {
String url = "http://localhost:7373/ccy_server/ccy";
//String params = "name=ccy&password=123";
String params = "{\"name\":\"陈昌圆\",\"password\":\"123\"}";
try {
String result = httpGetOrPost("POST", url, params);
System.out.println("client---result:"+result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static String httpGetOrPost(String type, String url, String params) throws Exception{
//get请求通过url传参(post可以通过url传参也可以将参数写在http正文传参)
if("GET".equals(type)){
if(url.contains("?")){
url += "&" + params;
}else{
url += "?" + params;
}
}
System.out.println("请求地址:" + url);
System.out.println("请求参数:" + params);
URL u = new URL(url);
/*
* 查看URL API 发现 openConnection方法返回为URLConnection
* HttpURLConnection为URLConnection的子类,有其更多的实现方法,
* 通常将其转型为HttpURLConnection
* */
HttpURLConnection httpConn = (HttpURLConnection) u.openConnection();
//设置请求方式 默认是GET
httpConn.setRequestMethod(type);
//写 默认均为false,GET不需要向HttpURLConnection进行写操作
if("POST".equals(type)){
httpConn.setDoOutput(true);
}
httpConn.setDoInput(true);//读 默认均为true,HttpURLConnection主要是用来获取服务器端数据 肯定要能读
httpConn.setAllowUserInteraction(true);//设置是否允许用户交互 默认为false
httpConn.setUseCaches(false);//设置是否缓存
httpConn.setConnectTimeout(5000);//设置连接超时时间 单位毫秒 ms
httpConn.setReadTimeout(5000);//设置访问超时时间
//setRequestProperty主要设置http请求头里面的相关属性
httpConn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
httpConn.setRequestProperty("accept", "*/*");
httpConn.setRequestProperty("Content-Type", "application/json");
//开启连接
httpConn.connect();
//post方式在建立连接后把头文件内容从连接的输出流中写入
if("POST".equals(type)){
//在调用getInputStream()方法中会检查连接是否已经建立,如果没有建立,则会调用connect()
OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
out.write(params);//将数据写入缓冲流
out.flush();//将缓冲区数据发送到接收方
out.close();
}
System.out.println("httpcode:"+httpConn.getResponseCode());
//读取响应,现在开始可以读取服务器反馈的数据
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer("");
String str;
while((str = br.readLine()) != null){
sb.append(str);
}
System.out.println(new Date()+"---响应:"+sb);
br.close();
httpConn.disconnect();
return sb.toString();
} }

get请求运行结果

客户端:

服务端:

post请求运行结果

客户端:

服务端:

2.DefaultHttpClient:需要导入三个包,httpclient-4.1.jar,httpcode-4.1.jar,commons-logging-1.1.1.jar,doPost方法是直接可以请求https,doPost2为的http方式,日后若有需要对接第三方接口需要https协议可以参考doPost方法

package com.lutongnet.HttpClient;

import java.security.cert.CertificateException;
public class HttpClientDemo { public static void main(String[] args) {
HttpClientDemo hct = new HttpClientDemo();
String url = "http://localhost:7373/ccy_server/ccy";
Map<String, String> map = new HashMap<String, String>();
map.put("name", "ccy");
map.put("password", "123");
String charset = "utf-8";
String result = hct.doPost(url, map, charset);
System.out.println("1.获取服务器端数据为:"+result);
String params = "{\"name\":\"陈昌圆\",\"password\":\"123\"}";
String result2 = hct.doPost2(url, params);
System.out.println("2.获取服务器端数据为:"+result2);
}
public String doPost2(String url, String content) {
System.out.println("请求地址:" + url);
System.out.println("请求参数:" + content);
String charsetName = "utf-8";
DefaultHttpClient httpclient = null;
HttpPost post = null;
try {
httpclient = new DefaultHttpClient();
post = new HttpPost(url);
post.setHeader("Content-Type", "application/json;charset=" + charsetName);
post.setEntity(new StringEntity(content, charsetName));
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
String rsp = EntityUtils.toString(entity, charsetName);
System.out.println("返回参数: "+rsp);
return rsp;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
httpclient.getConnectionManager().shutdown();
} catch (Exception ignore) {}
} }
public String doPost(String url,Map<String,String> map,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, (SchemeSocketFactory) ssf));
}
}
}

post请求运行结果

客户端:

服务端:

3.commons-httpclient需要导入两个包,commons-httpclient-3.0.jar,commons-codec-1.7.jar,这种方式是最简洁的,前后不到10行代码就解决了,不过需要注意的是设置正文编码,5种方式都可行,这种将参数拼接在http正文中,在服务端可以利用request.getParameter()方法获取参数,也可以用request.getInputStream()流的方式获取参数(这种方式如果参数中有中文的话,暂时没有找到解决乱码的方法)

package com.lutongnet.commonHttpclient;

import java.util.ArrayList;
import java.util.List; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams; public class CommonHttpClient {
public static void main(String[] args) {
String url = "http://localhost:7373/ccy_server/ccy";
List<String> params = new ArrayList<String>();
params.add("陳昌圓");
params.add("123");
CommonHttpClient chc = new CommonHttpClient();
String result = chc.getPostMethod(url, params);
System.out.println("commonHttpClient---->从服务端获取的数据:" + result);
}
private String getPostMethod(String url, List<String> params) {
String result = null;
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
//httpClient.getParams().setContentCharset("utf-8");
//httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
//postMethod.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
//postMethod.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE
// + "; charset=utf-8");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
postMethod.addParameter("name", params.get(0));
postMethod.addParameter("password", params.get(1));
try {
httpClient.executeMethod(postMethod);
result = postMethod.getResponseBodyAsString();
System.out.println("result:" + result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/*class ccyPostMethod extends PostMethod {
public ccyPostMethod(String url) {
super(url);
} @Override
public String getRequestCharSet() {
return "utf-8";
}
}*/ }

post请求运行结果

客户端:

服务端:

Web服务器与客户端三种http交互方式的更多相关文章

  1. python web编程-CGI帮助web服务器处理客户端编程

    这几篇博客均来自python核心编程 如果你有任何疑问,欢迎联系我或者仔细查看这本书的地20章 另外推荐下这本书,希望对学习python的同学有所帮助 概念预热 eb客户端通过url请求web服务器里 ...

  2. Tomcat、Apache、IIS这三种Web服务器来讲述3种搭建JSP运行环境的方法

    一.相关软件介绍 1. J2SDK:Java2的软件开发工具,是Java应用程序的基础.JSP是基于Java技术的,所以配置JSP环境之前必须要安装J2SDK. 2. Apache服务器:Apache ...

  3. Java Web开发Tomcat中三种部署项目的方法

    第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: <Context path="/hello" docBase ...

  4. WebService学习整理(一)——客户端三种调用方式整理

    1 WebService基础 1.1 作用 1,       WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...

  5. nginx反向代理后端web服务器记录客户端ip地址

    nginx在做反向代理的时候,后端的nginx web服务器log中记录的地址都是反向代理服务器的地址,无法查看客户端访问的真实ip. 在反向代理服务器的nginx.conf配置文件中进行配置. lo ...

  6. [Web 前端] 006 css 三种页面引入的方法

    1. 外链式 用法 step 1: 在 html 文档的 head 头部分写入下方这句话 <link rel="stylesheet" href="./xxx.cs ...

  7. 如何搭建一个WEB服务器项目(三)—— 实现安卓端联网登录

    安卓端调用服务器登录函数进行验证登录 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验 ...

  8. python一个简单的web服务器和客户端

    服务器:      当客户联系时创建一个连接套接字      从这个连接接收HTTP请求(*)      解释该请求所请求的特定文件      从服务器的文件系统获取该文件      并发送文件内容 ...

  9. redis数据库服务器开启的三种方式

    redis的启动方式1.直接启动  进入redis根目录,执行命令:  #加上‘&’号使redis以后台程序方式运行 1 ./redis-server & 2.通过指定配置文件启动  ...

随机推荐

  1. (使用STL自带的排序功能进行排序7.3.2)POJ 2092 Grandpa is Famous(结构体排序)

    /* * POJ_2092.cpp * * Created on: 2013年11月1日 * Author: Administrator */ #include <iostream> #i ...

  2. Android-Java控制多线程执行顺序

    功能需求: Thread-0线程:打印 1 2 3 4 5 6 Thread-1线程:打印1 1 2 3 4 5 6 先看一个为实现(功能需求的案例) package android.java; // ...

  3. applicationContext.xml 基本配置

    <!-- 头文件,主要注意一下编码 --><?xml version="1.0" encoding="UTF-8"?><beans ...

  4. git常用命令常用场景

    在使用git之前,一直用的是svn版本管理:与svn最大不同的是,git有两个仓库,一个是本地仓库,一个是服务器上共享的仓库:本地仓库是每个开发者自己独有的,即使commit提交也只是提交到本地仓库: ...

  5. SpringBoot2 使用Spring Session集群

    有几种办法: 1.扩展指定server利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/Jetty等Serv ...

  6. mac终端常用命令

    1.du #查看文件目录大小 示例:查看DataCenter目录下所有文件/文件夹的大小 everSeeker:DataCenter pingping$ -h .9G ./Books 1.2M ./C ...

  7. Postgres 的 Array 类型

    mysql 不支持 Array 类型 一.Postgres 原生SQL 适用场景:可以用于实现贴标签功能 1.定义 CREATE TABLE "Students" ( name V ...

  8. Others - On Duty

    On Duty This is xxx and will be duty engineer in the next week. Thanks. Here is a kindly reminder. T ...

  9. VNC远程连接阿里云Linux服务器 图形界面

    VNC 简介: VNC,全称:Virtual Network Computing,即虚拟网络计算机:分客户端和服务端,即VNC Viewer和VNC Server.它是一款远程控制的软件,一般用于远程 ...

  10. Git for Windows之推送本地版本库到远程仓库

    Git for Windows之基础环境搭建与基础操作中介绍了Git基本环境的构建与基本的操作.生成了一个本地git版本库,本文将介绍如何将这个版本库推送到远程仓库(码云,github也可以). 1. ...