后台发送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通用方法

相关推荐

  1. 后台发送GET/POST方法

    2024-03-22 15:50:06       42 阅读
  2. C#后台发送Get和Post请求的几种方法总结

    2024-03-22 15:50:06       53 阅读
  3. C#后台向某个网站发送Get或者Post请求

    2024-03-22 15:50:06       29 阅读
  4. ADO.Net前端页面调用后台方法使用

    2024-03-22 15:50:06       53 阅读
  5. Linux 在后台执行 shell 指令的方法

    2024-03-22 15:50:06       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 15:50:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 15:50:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 15:50:06       82 阅读
  4. Python语言-面向对象

    2024-03-22 15:50:06       91 阅读

热门阅读

  1. Qt Excel文件读写

    2024-03-22 15:50:06       38 阅读
  2. 9. Linux 信号详解

    2024-03-22 15:50:06       46 阅读
  3. 在Linux/Ubuntu/Debian中创建自己的命令快捷方式

    2024-03-22 15:50:06       42 阅读
  4. 以太网网络变压器

    2024-03-22 15:50:06       37 阅读
  5. nginx的location规则与其他功能

    2024-03-22 15:50:06       37 阅读
  6. [json.exception.type_error.316] invalid UTF-8 byte报错

    2024-03-22 15:50:06       43 阅读
  7. 单例设计模式:Python魔法中的唯一守护者

    2024-03-22 15:50:06       42 阅读
  8. 微信小程序网络通信

    2024-03-22 15:50:06       41 阅读