java中使用HttpRequest发送请求

使用mvn创建项目,导入HttpRequest的mvn包
该工具包,包括:get,post,put,delete 等请求方式。

    <dependency>
      <groupId>com.github.kevinsawicki</groupId>
      <artifactId>http-request</artifactId>
      <version>5.6</version>
    </dependency>

1、get方式

//请求参数
Map<String, String> map = new HashMap<>();

map.put("appKey", Const.HBASEAPPKEY);
map.put("itemId", "60246");
map.put("menu", "BASIC");

//请求地址
String url = "";

HttpRequest httpRequest = HttpRequest.get(url, map, Boolean.TRUE);
String result = httpRequest.body();

2、post方式

Map<String, String> map = new HashMap<>();

map.put("appKey", Const.HBASEAPPKEY);
map.put("orderIds", "33839");
map.put("channel", "alipay");
map.put("stageInfo", "0");

String url = "";

HttpRequest httpRequest = HttpRequest.post(url, map, Boolean.TRUE);
String result = httpRequest.body();

3、put方式

Map<String, String> map = new HashMap<>();

map.put("appKey", Const.HBASEAPPKEY);
map.put("orderIds", "33839");
map.put("channel", "alipay");

String url = "";

HttpRequest httpRequest = HttpRequest.put(url, map, Boolean.TRUE);
String result = httpRequest.body();

其他请求方式不再做解释,需要请自己完成。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web应用程序,可以使用HttpServletRequest对象来获取客户端提交的HTTP请求的相关信息。如果要在发送HTTP请求时获取HttpServletRequest对象,则需要在发送请求的代码创建一个HttpServletRequest对象并填充相关信息。这是不可能的,因为HttpServletRequest对象是由Web容器创建和维护的。 如果您想获取当前请求的HttpServletRequest对象,可以使用ThreadLocal来存储HttpServletRequest对象,然后在需要使用HttpServletRequest对象时从ThreadLocal获取。您可以创建一个Filter来将HttpServletRequest对象存储在ThreadLocal,然后在整个请求处理过程都可以访问该对象。以下是一个获取HttpServletRequest对象的示例代码: ``` public class RequestFilter implements Filter { private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<>(); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; requestHolder.set(httpRequest); try { chain.doFilter(request, response); } finally { requestHolder.remove(); } } public static HttpServletRequest getRequest() { return requestHolder.get(); } } ``` 在上面的代码,我们创建了一个名为RequestFilter的Filter,并将HttpServletRequest对象存储在ThreadLocal。在过滤器链的每个处理程序都可以使用RequestFilter.getRequest()方法来获取HttpServletRequest对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值