java使用httpcomponents发送get请求
一、适用场景
在ESTful webservice中,get方法一般都是用来获取数据。我们可以使用httpcomponents组件来完成调用。
如我们需要发起get请求,携带的参数都是附加到请求的url后面。
url:"http://www.xxxxx.com/message?id=000010"
二、代码示例
package com.demo.test; import java.io.IOException; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; public class Test2 { public static String sendInfo(String url, String param) {
String geturl = String.format("%s?id=%s", url, param);
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(geturl);
String responseContent = null; // 响应内容
CloseableHttpResponse response = null;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();// 响应体
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 正确返回
responseContent = EntityUtils.toString(entity, "UTF-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null)
response.close();
if (client != null)
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
} public static void main(String[] args) {
String param = "000010";
String result = sendInfo("http://www.xxxxx.com/message", param);
System.out.println(result);
}
}
请求成功后,后天会打印返回的信息。
java使用httpcomponents发送get请求的更多相关文章
- java 模拟浏览器发送post请求
java使用URLConnection发送post请求 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求 ...
- Java学习笔记--通过java.net.URLConnection发送HTTP请求
http://www.cnblogs.com/nick-huang/p/3859353.html 使用Java API发送 get请求或post请求的步骤: 1. 通过统一资源定位器(java.net ...
- 通过java.net.URLConnection发送HTTP请求的方法
一.前言 如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求. Java有原生的API可用于发送HTTP请求,即java.net.URL.java.net.UR ...
- 通过java.net.URLConnection发送HTTP请求(原生、爬虫)
目录 1. 运用原生Java Api发送简单的Get请求.Post请求 2. 简单封装 3. 简单测试 如何通过Java发送HTTP请求,通俗点讲,如何通过Java(模拟浏览器)发送HTTP请求.Ja ...
- java通过java.net.URL发送http请求调用接口
一般在*.html,*.jsp页面中我们通过使用ajax调用接口,这个是我们通常用的.对于这些接口,大都是本公司写的接口供自己调用,所以直接用ajax就可以.但是,如果是多家公司共同开发一个东西,一个 ...
- java 实现HttpRequest 发送http请求
package com.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- 【java】httpclient的使用之java代码内发送http请求
场景: 需要本项目发送HTTP请求到另一个项目中,处理完成返回值给本项目. 1.本项目引入架包 <!-- httpclient 后台发送http请求--> <dependency&g ...
- Java 使用代理发送Http请求 (将Http请求代理Https请求)
package com.test.porxy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- Java使用FeignClient发送HTTP 请求
使用FeignClient发送HTTP请求1.添加依赖<!-- spring cloud jar--><dependency> <groupId>org.sprin ...
随机推荐
- Python+Selenium进行UI自动化测试项目中,常用的小技巧4:日志打印,longging模块(控制台和文件同时输出)
在前段时间,为了给项目中加入日志功能,就想到了 logging 模块,百度logging一大推,都是各种复制的,并没有找到自己想要的结果:我的目的很简单,就是:在把日志写入文件的同时在控制台输出,更加 ...
- jquery选择器(原创)
jquery选择器大方向可以分为这样: 下面我们先来看看基本选择器总的CSS选择器: 1.标签选择器: $("element") 其中,参数element,表示待查找的HTML标记 ...
- 快速幂 --- CSU 1556: Jerry's trouble
Jerry's trouble Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1556 Mean: 略. ana ...
- 关于SqlServer2012本地帮助文档安装失败问题
由于本人在前一段时间安装了SqlServer2012,安装后没有自带本地帮助文档,因为新的数据库版本微软捆绑了一个本地帮助文档查看器软件,所以在这个软件里面选择联机安装的时候,总是下载不成功,原因你们 ...
- dictionary 添加数据
var empList = from p in customers select new { p.Personnel_ID, p.PersonName }; var empTempList = emp ...
- LeetCode129:Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- Servlet-Jsp
Jsp实际就是Servlet. 我们访问Http://localhost:8080/Web/index.jsp的流程: 1 [jsp文件名].jsp转义为[jsp文件名_jsp].java,文件存储在 ...
- ADO.NET 完整的修改和删除
namespace 完整修改{ class Program { static void Main(string[] args) { bool has = false; Console.Write(&q ...
- CentOS安装Erlang
1.首先要安装编译源码用的编译器gcc&g++,安装方式很简单,先用yum search gcc搜索出包,然后选择适合自己的版本复制全名,用yum intall gcc_XXX来进行安装即可. ...
- Visual Studio添加dll程序集引用操作步骤
Visual Studio 中添加引用的操作: 在“解决方案资源管理器”中,先右击项目图标,在弹出菜单选择“添加引用...” 然后在弹出的窗口中选择所要添加的选项,点击确定就可以了. 原文:http: ...