HttpClient 网络请求

一、GET请求
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public static String doGet(String url) { -> 参数拼接在URL中
    String result = "";
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        }
        httpResponse.close();
        httpClient.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return result;
}
二、POST请求
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;

public static String doPost(String url, String json) { -> 参数拼接在URL中
    String result = "";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
    try {
        StringEntity stringEntity = new StringEntity(json, "utf-8"); -> JSON对象
        stringEntity.setContentType("text/json");
        Header header = new BasicHeader(HTTP.CONTENT_TYPE, "application/json");
        stringEntity.setContentEncoding(header);
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

@RequestMapping("/post")
@ResponseBody
public void post(HttpServletRequest request) { -> 获取StringEntity的参数
    try{
        Reader reader = new InputStreamReader(request.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        while((line = bufferedReader.readLine())!=null){
            stringBuilder.append(line);
        }
        String json = stringBuilder.toString();
        JSONObject object = JSON.parseObject(json);
    }catch (Exception e){
        e.printStackTrace();
    }
}
三、请求来源
public boolean JudgeIsMoblie(HttpServletRequest request) {
    boolean moblie = false;
    String[] agents = {"iphone", "android", "phone", "mobile", "wap", "netfront", "java",
            "opera mobi", "opera mini", "ucweb", "windows ce", "symbian", "series", "webos",
            "sony", "blackberry", "dopod", "nokia", "samsung", "palmsource", "pieplus",
            "meizu", "midp", "cldc", "motorola", "foma", "docomo", "up.browser", "up.link",
            "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos", "techfaith",
            "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
            "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-",
            "longcos", "pantech", "gionee", "xda", "xda-", "Googlebot-Mobile", "portalmmm",
            "jig browser", "hiptop", "benq", "haier", "^lct", "320x320", "240x320", "176x220",
            "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
            "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno",
            "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
            "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-",
            "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox",
            "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar",
            "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-",
            "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
            "wapr", "webc", "winw"};
    if (request.getHeader("User-Agent") != null) {
        for (String agent : agents) {
            if (request.getHeader("User-Agent").toLowerCase().contains(agent)) {
                moblie = true;
                break;
            }
        }
    }
    return moblie;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容