中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java發(fā)送GET、POST請(qǐng)求代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

一、創(chuàng)建一個(gè)servlet來(lái)接收get或post請(qǐng)求

package guwen;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

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

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class TestServlet extends HttpServlet{

    /**
     * 接收get請(qǐng)求
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        System.out.println(req.getQueryString());  //打印參數(shù)
        PrintWriter out = resp.getWriter();
        out.print("響應(yīng)");  //響應(yīng)
    }

    /**
     * 接收post請(qǐng)求
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        System.out.println(req.getQueryString());  //打印參數(shù)
        InputStream inputStream = req.getInputStream();
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(inputStream);  //報(bào)文體
            System.out.println(document.asXML());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        PrintWriter out = resp.getWriter();
        out.print("響應(yīng)");  //響應(yīng)
    }

    
}

二、發(fā)送請(qǐng)求

package guwen;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


public class ConnectionUtil {
    
    public static void main(String[] args) throws ClientProtocolException, IOException {
        //sent get
        doGet("http://localhost:8088/sentTest/test?p=123");
        //sent post
        doPost("http://localhost:8088/sentTest/test?p=321","<xml><a>aa</a><b>哈</b></xml>");
    }
    
    /**
     * 發(fā)送get請(qǐng)求
     * @throws IOException 
     */
    public static String doGet(String url) throws ClientProtocolException, IOException {
        
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();  
        
        HttpGet httpGet = new HttpGet(url);     
  
        //配置請(qǐng)求的超時(shí)設(shè)置  
        RequestConfig requestConfig = RequestConfig.custom()    
                .setConnectionRequestTimeout(50)  
                .setConnectTimeout(50)    
                .setSocketTimeout(50).build();    
        httpGet.setConfig(requestConfig);   
          
        CloseableHttpResponse response = null;
        String res = null;
        try {
            response = httpClient.execute(httpGet);   //發(fā)送請(qǐng)求
            System.out.println("StatusCode -> " + response.getStatusLine().getStatusCode());  
            HttpEntity entity = response.getEntity();          
            res = EntityUtils.toString(entity,"utf-8");   
            System.out.println(res);  
        } catch (ClientProtocolException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally{
            httpGet.releaseConnection(); 
        }
          
        return res;
    }
    
    
    
    /**
     * 發(fā)送get請(qǐng)求
     * @throws IOException 
     */
    public static String doPost(String url,String body) throws IOException {
        
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();  
        HttpPost httpPost = new HttpPost(url);
        //配置請(qǐng)求的超時(shí)設(shè)置  
        RequestConfig requestConfig = RequestConfig.custom()    
                .setConnectionRequestTimeout(50)  
                .setConnectTimeout(50)    
                .setSocketTimeout(50).build();    
        httpPost.setConfig(requestConfig); 
        
        CloseableHttpResponse response = null;
        String res = null;
        try {
            if (body != null) {  //設(shè)置報(bào)文體 設(shè)置編碼為 UTF-8
                StringEntity entity = new StringEntity(body, "UTF-8");
                httpPost.setEntity(entity);
            }
            response = httpclient.execute(httpPost);  //發(fā)送請(qǐng)求
            System.out.println(response.toString());  
            
            HttpEntity entity = response.getEntity();  
            res = EntityUtils.toString(entity, "utf-8");  
            System.out.println(res);  
        } catch (ClientProtocolException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally{
            httpPost.releaseConnection();
        }
        
        return res;
        
        
    }
    
    
}


標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:使用java開(kāi)源項(xiàng)目Jsoup抓取遠(yuǎn)程圖片

下一篇:使用java.util.zip壓縮文件夾,支持加密,增加描述