java http请求实现_java实现http请求之HttpURLConnection

JDK网络类Java.net.HttpURLConnection继承自URLConnection,用于向指定网站发送GET请求、POST请求。

它在URLConnection的基础上提供了如下便捷的方法:

int getResponseCode(); // 获取服务器的响应代码。 String getResponseMessage(); // 获取服务器的响应消息。 String getResponseMethod(); // 获取发送请求的方法。 void setRequestMethod(String method); // 设置发送请求的方法。

实现过程(步骤5、6、7、8仅适用post请求,get请求忽略):

1、创建远程连接2、设置连接方式

3、设置连接超时时间

4、设置响应读取时间

5、当向远程服务器传送数据/写数据时,需要设置为true(setDoOutput)

6、当前向远程服务读取数据时,设置为true,该参数可有可无(setDoInput)

7、设置传入参数的格式:(setRequestProperty)

8、设置鉴权信息:Authorization:(setRequestProperty)

9、设置参数

10、发起请求

11、获取请求数据

12、关闭连接

代码示例(以post为例):

HttpURLConnection的connect()函数,实际上只是建立了一个与服务器的TCP连接,并没有实际发送HTTP请求。HTTP请求实际上直到我们获取服务器响应数据(如调用getInputStream()、getResponseCode()等方法)时才正式发送出去。

对HttpURLConnection对象的配置都需要在connect()方法执行之前完成。

HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置超时(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。

代码转载自:https://riemann.blog.csdn.net/article/details/90539829?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

public static String doPost(String httpUrl, @Nullable String param) {

StringBuffer result = new StringBuffer();

//连接

HttpURLConnection connection = null;

OutputStream os = null;

InputStream is = null;

BufferedReader br = null;

try {

//创建连接对象

URL url = new URL(httpUrl);

//创建连接

connection = (HttpURLConnection) url.openConnection();

//设置请求方法

connection.setRequestMethod("POST");

//设置连接超时时间

connection.setConnectTimeout(15000);

//设置读取超时时间

connection.setReadTimeout(15000);

//DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读

入,此外发送post请求必须设置这两个

//设置是否可读取

connection.setDoOutput(true);

connection.setDoInput(true);

//设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0;

Windows NT 5.1; SV1)");

connection.setRequestProperty("Content-Type", "application/json;charset=utf-

8");

//拼装参数

if (null != param && param.equals("")) {

//设置参数

os = connection.getOutputStream();

//拼装参数

os.write(param.getBytes("UTF-8"));

}

//设置权限

//设置请求头等

//开启连接

//connection.connect();

//读取响应

if (connection.getResponseCode() == 200) {

is = connection.getInputStream();

if (null != is) {

br = new BufferedReader(new InputStreamReader(is, "GBK"));

String temp = null;

while (null != (temp = br.readLine())) {

result.append(temp);

result.append("\r\n");

}

}

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

//关闭连接

if(br!=null){

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(os!=null){

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//关闭连接

connection.disconnect();

}

return result.toString();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值