以前也发过类似于这样的文章。但是这个正规。是Apache下的项目。
直接看代码:
/** * Mainbo.com Inc. * Copyright (c) 2015-2017 All Rights Reserved. */ package com.mainbo.jy.resdown.Main; import java.io.File; 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.NameValuePair; 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.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * <pre> * * </pre> * * @author huangjunhua * @version $Id: Main.java, v 1.0 2015年5月13日 下午12:01:09 huangjunhua Exp $ */ public class Main { /** * 返回接口信息 * @param strUrl,接口参数 * paramStr 参数信息 * @return 服务器返回信息 * @throws Exception */ public static String postHttp(String url,String paramStr){ String content=""; try { // 创建默认的httpClient实例. HttpClient httpclient =new DefaultHttpClient(); // 创建httpget. HttpPost httpPost = new HttpPost("http://localhost:8080/TestWeb/s"); System.out.println("executing request " + httpPost.getURI()); //传递参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("a", "中文")); nvps.add(new BasicNameValuePair("password", "secret20")); //设置参数的编码 UrlEncodedFormEntity encodedFormEntity=new UrlEncodedFormEntity(nvps,"UTF-8"); //设置发送参数的实体 httpPost.setEntity(encodedFormEntity); //执行post请求 HttpResponse httpResponse=httpclient.execute(httpPost); //返回来的实体 HttpEntity entity = httpResponse.getEntity(); //响应状态 System.out.println("status:" + httpResponse.getStatusLine()); //判断响应实体是否为空 if (entity != null) { System.out.println("contentEncoding:" + entity.getContentType());//返回参数的编码 //返回参数的内容 System.out.println("response content:" + EntityUtils.toString(entity)); content=EntityUtils.toString(entity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return content; }
下面是接受的servlet代码:
import java.io.BufferedReader; 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; public class TestServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("*******"); String message = req.getParameter("a"); String password = req.getParameter("password"); System.out.println(message); System.out.println(password); req.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain; charset=utf-8");//设那边置编码防止http那边乱码 resp.setCharacterEncoding("UTF-8"); PrintWriter out=resp.getWriter (); out.print("欢迎:"+message); } }