java 获取url地址返回的数据

参考自:http://blog.csdn.net/daydayupzzc/article/details/38866489

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Url {

    public static void main(String[] args) throws Exception {
        String url="http://flash.weather.com.cn/wmaps/xml/chengdu.xml";
        getData(url);
    }

    public static String getData(String urlString) throws Exception {
        String res = "";
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            URLConnection conn = (URLConnection)url.openConnection();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            System.out.println("创建url!");
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("分别获取每行内容!:"+line);
                res += line;
            }
            reader.close();
        } catch (Exception e) {
            e.getMessage();
        }
        System.out.println(res);
        return res;
    }
}

返回结果:
运行结果
原网页内容:
原网页内容
接下来就可以解析其中自己需要的内容了(上面例子中返回的数据是xml格式的字符串,如果需要返回数据流请参考上面原教程)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Java可以通过以下步骤获取response返回数据: 1. 获取HttpURLConnection对象,该对象表示与服务器的连接。 2. 设置请求方法和请求头。 3. 发送请求并获取服务器返回的状态码。 4. 如果状态码为200,则获取服务器返回数据流。 5. 读取数据流并将其转换为字符串。 6. 关闭连接。 示例代码如下: ``` URL url = new URL("http://www.example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-Type", "application/json"); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); String responseData = response.toString(); System.out.println(responseData); } conn.disconnect(); ``` ### 回答2: 在Java中,获取response返回数据有几种常用的方法。其中最常见的方法是通过HttpURLConnection类或HttpClient类来实现。 1.使用HttpURLConnection类 HttpURLConnection类是Java中处理HTTP请求的标准类之一,它提供了获取response数据的方法,以下是获取response数据的基本步骤: 1)创建HttpURLConnection对象 URL url = new URL("http://www.example.com/getData"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 2)设置请求方法和请求头 conn.setRequestMethod("GET"); conn.setRequestProperty("Content-type", "application/json"); 3)获取response数据 InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer responseContent = new StringBuffer(); while ((line = reader.readLine()) != null) { responseContent.append(line); } String responseData = responseContent.toString(); 2.使用HttpClient类 HttpClient类是一个HTTP客户端库,它也可以用于获取response数据。使用HttpClient类获取response数据的基本步骤如下: 1)创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); 2)创建HttpGet对象并设置请求头 HttpGet httpGet = new HttpGet("http://www.example.com/getData"); httpGet.addHeader("Content-type", "application/json"); 3)执行请求并获取response数据 CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String responseData = EntityUtils.toString(entity); 以上是获取response数据的两个常见方法,另外还有其他方法,如使用HttpURLConnection的getResponseCode()方法获取HTTP状态码。不管哪种方法,获取response数据前都必须先建立HTTP连接并向目标服务器发送HTTP请求,然后获取服务器返回的response数据。 ### 回答3: Java是一种广泛使用的编程语言,它可以与各种类型的应用程序进行交互,包括web应用程序。 当我们使用java编写web应用程序时,通常需要与外部系统进行交互,并获取返回数据。在web应用程序中,能够获取到的来自其他系统的返回数据主要是HTTP响应。HTTP响应在Java中也称为HttpResponse, 获取HttpResponse中的数据需要: 1.获取HttpResponse对象 在Java中,可以通过使用HttpURLConnection或者HttpClient类来实现与其他系统的连接,并获取HttpResponse对象。这两个类的使用方式不同,但最终都会返回一个HttpResponse对象,我们需要通过该对象来获取返回数据。下面分别介绍使用HttpURLConnection和HttpClient获取HttpResponse对象的方法。 2.获取HttpResponse的返回码 HttpResponse对象包含了来自外部系统的所有响应信息,如响应头、返回码、返回数据等。通常要获取HttpResponse的返回码,以了解外部系统是否成功响应了请求。可以通过HttpResponse对象提供的方法来获取返回码,例如:HttpResponse.getStatusLine().getStatusCode()。 3.获取HttpResponse的返回数据 获取HttpResponse对象后,需要提取其中的信息,其中包括响应头和响应体。在Java中,获取HttpResponse的响应体数据的方法有多种。在使用HttpClient获取HttpResponse对象时,可以使用HttpResponse.getEntity()方法获取响应体,并通过EntityUtils.toString()方法将其转换为字符串。在使用HttpURLConnection获取HttpResponse对象时,可以使用InputStream对象来获取响应体数据,并将其转换为字符串。 总的来说,在Java获取HttpResponse的返回数据并不困难,主要通过获取HttpResponse对象、获取返回码和获取返回数据三个步骤来实现。根据具体的需求和实际情况,可以选择使用不同的类库来获取HttpResponse对象,并使用不同的方法来获取其中的返回数据
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值