0

マルチパート ライブラリには Apache を使用する必要がありますHttpComponents。ただし、と ストッククラスでHttpMime明らかにまったく同じ呼び出しを行うと、次のように失敗します。HttpComponentsjava.netHttpComponents

private void getUrl(URI targeturl, String token) throws IOException {

    String AUTH = "Authorization";
    String OAUTH = "OAuth ";

    System.out.println("Impl1:");
    HttpGet htg = new HttpGet(targeturl);
    htg.addHeader(AUTH, OAUTH + token);
    HttpResponse response = new DefaultHttpClient().execute(new HttpGet(targeturl));
    System.out.println(response);
    String resp = EntityUtils.toString(response.getEntity());
    System.out.println(resp);

    System.out.println("\nImpl 2:");
    HttpURLConnection conn = (HttpURLConnection) uriToUrl(targeturl).openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty(AUTH, OAUTH + token);
    System.out.print(conn.getResponseCode());
    System.out.println(" " + conn.getResponseMessage());

    StringWriter writer = new StringWriter();
    IOUtils.copy(conn.getInputStream(), writer);
    System.out.println(writer.toString());
}

//for known good uris ONLY
private URL uriToUrl(URI uri) {
    try {
        return uri.toURL();
    } catch (MalformedURLException mue) {
        throw new Error(mue); //something is seriously fuxxored
    }
}

結果の出力は次のとおりです。

Impl1:
HTTP/1.1 401 Unauthorized [Access-Control-Allow-Headers: Authorization, Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS, Access-Control-Allow-Origin: *, Content-Length: 51, Content-Type: application/json, Date: Sat, 20 Jul 2013 00:03:42 GMT]
{"status":401,"data":null,"error":["Unauthorized"]}

Impl 2:
200 OK
{"status":200,"data":{...},"error":null}

HttpComponents はクライアント 4.2.5、コア 4.2.4、java は 1.6.0-24 です。セットアップは私には同じように見えますが、いくつかの違いがあるに違いありません。それは何ですか?

4

1 に答える 1

0

それはばかげたコーディングの間違いです。

HttpResponse response = new DefaultHttpClient().execute(new HttpGet(targeturl));

する必要があります

HttpResponse response = new DefaultHttpClient().execute(htg);
于 2013-07-20T02:59:27.237 に答える