• Http发送请求


    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    package com.amdox.mobile.utils;
    
    import java.io.IOException;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    
    public class HttpClientHelper {
        public static String sendPost(String urlParam) throws HttpException, IOException {
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建post请求方法实例对象
            PostMethod postMethod = new PostMethod(urlParam);
            // 设置post请求超时时间
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
            postMethod.addRequestHeader("Content-Type", "application/json");
    
            httpClient.executeMethod(postMethod);
    
            String result = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
            return result;
        }
        public static String sendGet(String urlParam) throws HttpException, IOException {
            // 创建httpClient实例对象
            HttpClient httpClient = new HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建GET请求方法实例对象
            GetMethod getMethod = new GetMethod(urlParam);
            // 设置post请求超时时间
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
            getMethod.addRequestHeader("Content-Type", "application/json");
    
            httpClient.executeMethod(getMethod);
    
            String result = getMethod.getResponseBodyAsString();
            getMethod.releaseConnection();
            return result;
        }
        public static void main(String[] args) throws HttpException, IOException {
            String url ="https://www.baidu.com/";
            System.out.println(sendGet(url));
            System.out.println(sendPost(url));
        }
    }
  • 相关阅读:
    Linq调试实时输出信息扩展方法(摘抄)
    RSA签名和验证数据
    Vue+abp微信扫码登录
    vue学习笔记4
    icon共享网站 可以获得wpf里用的Geometry
    wpf 父控件和子控件 各自触发鼠标按下事件
    C# Timespan Tostring 时分秒格式
    新公司第二天
    C# 反转单向链表
    经纬度转数字经纬度时,如果是负数
  • 原文地址:https://www.cnblogs.com/yeg0zj/p/12793929.html
Copyright © 2020-2023  润新知