zoukankan      html  css  js  c++  java
  • Java spring boot HttpUTILS

    import java.io.IOException;
    import java.util.Map;

    import com.alibaba.fastjson.JSON;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;

    public class HttpClientUtil {

    public static String doPostJson(String url, String json) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String resultString = "";
    try {
    HttpPost httpPost = new HttpPost(url);
    StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
    httpPost.setEntity(entity);
    response = httpClient.execute(httpPost);
    resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    response.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return resultString;
    }

    public static String postFormEvidence(String SCANINFO_URL,
    String parameters){
    try {
    String postURL =SCANINFO_URL;
    PostMethod postMethod = null;
    postMethod = new PostMethod(postURL) ;
    postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
    //参数设置,需要注意的就是里边不能传NULL,要传空字符串

    NameValuePair[] data = {
    new NameValuePair("parameters",parameters)
    };

    postMethod.setRequestBody(data);

    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
    int response = httpClient.executeMethod(postMethod); // 执行POST方法
    String result = postMethod.getResponseBodyAsString() ;
    System.out.println(response);
    System.out.println(result);

    return result;
    } catch (Exception e) {
    // logger.info("请求异常"+e.getMessage(),e);
    throw new RuntimeException(e.getMessage());
    }
    }

    public static String doPostJsonform(String url, String json) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String resultString = "";
    try {
    HttpPost httpPost = new HttpPost(url);

    StringEntity entity = new StringEntity(json, ContentType.APPLICATION_FORM_URLENCODED);
    System.out.println(JSON.toJSONString(entity));

    httpPost.setEntity(entity);

    response = httpClient.execute(httpPost);
    resultString = EntityUtils.toString(response.getEntity(), "utf-8");
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    response.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return resultString;
    }
    }

    再牛逼的梦想,也抵不住我傻逼似的坚持!别在该奋斗的年纪,贪图安逸。 今天多学一些知识,明天开发的速度就更快一下。后天你就会变得更好。
  • 相关阅读:
    HeapSpray初窥(2014.12)
    CVE-2014-4115漏洞分析(2014.11)
    【原创】oracle提权执行命令工具oracleShell v0.1
    【原创】贴个dirtycow(脏牛漏洞)不死机的exploit
    【CVE-2016-10009】OpenSSH < 7.4
    关于elasticsearch和kibana的时区和日期问题
    这是我写过的最长的正则表达式,没有之一
    三生缘
    【原创】JEECMS v6~v7任意文件上传漏洞(2)
    【原创】JEECMS v6~v7任意文件上传漏洞(1)
  • 原文地址:https://www.cnblogs.com/LowKeyCXY/p/13671948.html
Copyright © 2011-2022 走看看