简体   繁体   English

为什么在使用“ apache httpclient”和“ httpurlconnection”时http状态码不同?

[英]Why http status code is different when using “apache httpclient” and “httpurlconnection”?

I am now implementing weibo oauth2 login. 我现在正在实现微博oauth2登录。 I used two method - apache httpclient and httpurlconnection. 我使用了两种方法-Apache httpclient和httpurlconnection。 But HTTP response code is different in two case. 但是HTTP响应代码在两种情况下是不同的。

String accessToken = "***";
String weiboUid = "***";

try {
        URL url = new URL("https://api.weibo.com/2/users/show.json?access_token=" + accessToken + "&uid=" + weiboUid);
        conn2 = (HttpURLConnection) url.openConnection();
        conn2.setConnectTimeout(10000);
        conn2.setDoInput(true);
        conn2.setDoOutput(true);
        conn2.setRequestMethod("GET");

        outputStream = conn2.getOutputStream();
        outputStream.flush();

        int responseCode = conn2.getResponseCode();

        StringBuilder responseReq = new StringBuilder();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
            while ((line = br.readLine()) != null) {
                responseReq.append(line).append("\n");
            }
            br.close();
        } else {
            throw new Exception("data not exist!");
        }

        String result = responseReq.toString().trim();
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(result).getAsJsonObject();
        String nickname = jsonObject.get("screen_name").getAsString();

        System.out.println(nickname);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (conn2 != null)
            conn2.disconnect();
    }

In this case, responseCode is 405 and excuted " throw new Exception("data not exist!"); ". 在这种情况下,responseCode为405并被“ throw new Exception("data not exist!"); “。

But when I use apache httpclient library, response code is 200 and I can get the login information. 但是,当我使用apache httpclient库时,响应代码为200,我可以获得登录信息。

String accessToken = "***";
String weiboUid = "***";
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("https://api.weibo.com/2/users/show.json?access_token=" + accessToken + "&uid=" + weiboUid);

    try {
        HttpResponse response = client.execute(request);
        System.out.println("Response Code : " +
                response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

What is the difference between apache httpclient and httpurlconnection? apache httpclient和httpurlconnection有什么区别?

GET requests don't have content. GET请求没有内容。

Remove conn2.setDoOutput(true); 删除conn2.setDoOutput(true); and all lines using outputStream . 和所有行使用outputStream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 11 及更高版本中使用 HttpClient 时如何跟踪 HTTP 303 状态代码? - How to follow-through on HTTP 303 status code when using HttpClient in Java 11 and later? 使用Apache Httpclient时,有没有一种获取下载状态的方法? - When using Apache Httpclient , is there a way to get downloading status? 为什么我只在使用 Java HTTP 客户端时得到 HTTP 响应状态码 451? - Why do I get HTTP Response Status Code 451 only when using the Java HTTP Client? HttpUrlConnection返回-1状态代码 - HttpUrlConnection returns -1 status code 应该不鼓励使用java.net.HttpURLConnection,因为org.apache.http.client.HttpClient更好吗? - Should use of java.net.HttpURLConnection be discouraged as org.apache.http.client.HttpClient is way better ? 当服务器返回状态码 400 时使用 HttpURLConnection 检索响应内容 - Retrieve response content using HttpURLConnection when server returns status code 400 (Apache httpclient)3个代码有什么不同? - (apache httpclient) What is different thing in 3 code? 服务器使用 httpurlconnection 发出 http 头请求时响应代码 400 - Server responding with response code 400 when making http head request using httpurlconnection httpURLConnection vs apache commons http - httpURLConnection vs apache commons http 如何使用HttpURLConnection以与Apache HttpClient lib相同的方式发出POST请求 - How can I make a POST request in the same manner as Apache HttpClient lib, using HttpURLConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM