新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在java后台发起一个get请求并获取响应数据

这期内容当中小编将会给大家带来有关怎么在java后台发起一个get请求并获取响应数据,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

站在用户的角度思考问题,与客户深入沟通,找到梓潼网站设计与梓潼网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都网站建设、企业官网、英文网站、手机端网站、网站推广、主机域名网站空间、企业邮箱。业务覆盖梓潼地区。

package com.jl.chromeTest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

/**
 * get请求测试
 */
public class Test {

 @org.junit.Test
  public void test() throws Exception{
  String result = get("http://www.baidu.com");
  System.out.println("result====="+result);
 }

 /**
  * get请求
  * @param url
  * @return
  * @throws Exception
  */
 public String get(String url) throws Exception {
  String content = null;
  URLConnection urlConnection = new URL(url).openConnection();
  HttpURLConnection connection = (HttpURLConnection) urlConnection;
  connection.setRequestMethod("GET");
  //连接
  connection.connect();
  //得到响应码
  int responseCode = connection.getResponseCode();
  if (responseCode == HttpURLConnection.HTTP_OK) {
   BufferedReader bufferedReader = new BufferedReader(new InputStreamReader
     (connection.getInputStream(), StandardCharsets.UTF_8));
   StringBuilder bs = new StringBuilder();
   String l;
   while ((l = bufferedReader.readLine()) != null) {
    bs.append(l).append("\n");
   }
   content = bs.toString();
  }
  return content;
 }
}

结果如图:

怎么在java后台发起一个get请求并获取响应数据

怎么在java后台发起一个get请求并获取响应数据

上述就是小编为大家分享的怎么在java后台发起一个get请求并获取响应数据了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


名称栏目:怎么在java后台发起一个get请求并获取响应数据
文章来源:http://www.wjwzjz.com/article/ihehds.html
在线咨询
服务热线
服务热线:028-86922220
TOP