Java类发送post请求

普通JAVA类发送post请求,接收请求的相应

1.普通java类

package httpConTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpConTest {
    /**
     * 向指定 URL 发送POST方法的请求
     * 
     * @param url    发送请求的 URL
     * @param param  请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);		 
            HttpURLConnection  conn = (HttpURLConnection)realUrl.openConnection();	// 打开和URL之间的连接
            
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);

            out = new PrintWriter(conn.getOutputStream());	// 获取URLConnection对象对应的输出流
            out.print(param);	 // 发送请求参数
            out.flush();		// flush输出流的缓冲
            
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));	// 定义BufferedReader输入流来读取URL的响应
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }    
	
    
    public static void main(String[] args) throws Exception {
    	
        //发送 POST 请求
        String sr=sendPost("http://localhost:8887/test/TestServlet", "name=zhangsan&password=888888");
        System.out.println(sr);
    }
}

 

2.接收请求的servlet

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String name = request.getParameter("name");
		String password = request.getParameter("password");

		System.out.println(name);
		System.out.println(password);
		
		response.setHeader("Content-type","text/html;charset=GBK");//向浏览器发送一个响应头,设置浏览器的解码方式为UTF-8
	    String data = "中国";		//返回给客户端的数据
	    OutputStream stream = response.getOutputStream();
	    stream.write(data.getBytes("GBK"));
	}

    public TestServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
}

  

你可能感兴趣的:(java)