当前位置:   article > 正文

后台发送GET/POST方法

后台发送GET/POST方法

前言:

1,get请求

2,post请求

3,post,get通用方法

4,其他的get,post写法

正文:

1,get请求

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

// HTTP GET request
public static String SendGet(String url) {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    
    try {
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            logger.error("获取SendGet失败:" + method.getStatusLine());
        }
        
        return method.getResponseBodyAsString();
    } catch (HttpException e) {
        logger.error("获取SendGet失败: Fatal protocol violation", e);
    } catch (IOException e) {
        logger.error("获取SendGet失败: transport error", e);
    } finally {
          method.releaseConnection();
    }
    
    return "";
}

2,post请求

// HTTP POST
public static String SendPOST(String url) {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    
    try {
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            logger.error("获取SendPOST失败:" + method.getStatusLine());
        }
        
        return method.getResponseBodyAsString();
    } catch (HttpException e) {
        logger.error("获取SendPOST失败: Fatal protocol violation", e);
    } catch (IOException e) {
        logger.error("获取SendPOST失败: transport error", e);
    } finally {
          method.releaseConnection();
    }
    
    return "";
}

3,post,get通用方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/267189
推荐阅读
相关标签
  

闽ICP备14008679号