Java读取Url请求获得返回数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010679782/article/details/51443858

代码如下:

public String urlString(String url) {
        String res = "";
        try {
            URL urls = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line;
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("res:" + res);
        return res;
    }

ok

展开阅读全文

为什么HttpClient请求返回400,URL请求返回302正常?

02-18

今天在做http请求的时候,发现了一个很别扭的地方。rn我使用android在请求广告资源的时候,返回给我400(),我以为是服务的问题。rn没太在意,可以后来用浏览器打开这个地址,发现浏览器返回的code是302,然后再跳一次跳转就可以跳转到200正常的界面。rn所以很纳闷。rn后来用URL进行请求,发现也能返回正常的code:200rn很晕。rnURL请求:rnHttpURLConnection openConnection = (HttpURLConnection) u.openConnection(); rnint responseCode = openConnection.getResponseCode();rnInputStream inputStream = openConnection.getInputStream();rnSystem.out.println(responseCode);rnrnClient请求:rnHttpClient client=new DefaultHttpClient();rnHttpGet get = new HttpGet(url);rnHttpResponse response = client.execute(get);rnint statusCode = response.getStatusLine().getStatusCode();rnrnrnrnurl地址:rnhttp://mobads.baidu.com/ad.html?url=http%3A%2F%2Fm.baidu.com%2Fcpro.php%3Fa0fK000cFDtp14ipxciFcucWgFoHy2H9iL-QuuIcNAkYIMRiaPQiWnmR6W2aNPD15t3bHYIBjLSFbvjfpThNBsKZLbZAU51-HXSkBpuqvKBXOMaqOkbYINsLI1jq.Db_ipx2roRhF596OlZextW6H7sEyFBOg_vTyyxZ-9k1QjPakblN0.IgF_5y9YIZ0lQzqLILT8mh7GuZR8mvqVQsKYUHYs0Zwd5H00IvsqP6KWUMw85yFhrj61myDLgvPsT6KYI1Yz0A7bmvk9TLnqn0KzT1Ys0APYUHdBmy-bIRPdTLw2Tvq80A49IZRqr0KGIA-b5HD10A-Ypy4hUv-b5H00uLKGujYs0ZF-uMKGujYs0APsThqGujY0mMNbuvNYgvN3TA-b5H00my-s5HNGU17hPWPNmvP80ZNGTjd9pAPEPAkRn-Pzu6KWpjY0IA7z5Hc0uZws5HD0TvN_UANzgv-b5Hc0pgPxmgKs5HD0mgKsgv-b5HnznHn1P1mkPWT0mLN1IjdlpZN9UyqB0A4-I7qYXgK-5HD0Uhqs5Hfvnj0s0A7bmhkEmvVxpvN45H00pgPxIv-zuyk-TLnqnfKLmgKxIZ-suHYs0AdEmh-_uNqsUA7YuhqzUHYz0AdETdqvugcqnB31QW00TAsqn0K_XAcqn0KVm1YkuHfYmhn3rfKVIWYk0A4vTjYsQW0snj0snj0s0AT45HD0uh-zTLwxThNMpyq85Hc0TvNWUv4bgLF-uv-EUWY3n1f0TLPs5Hc0TLPsnWYs0ZwYTjY10AwGTLws5H00mycqn0KWThnqnW64PHf0%26__mobads_cookie%3D%26__mobads_sid%3D723ff9d8572510f2&sn=863637021737296&clk=1 论坛

没有更多推荐了,返回首页