2016-03-23 14 views
6

私はHttpConnectを使用していて、サーバーから何らかのトークンを取得しようとしています。私は応答を取得しようとするたびに。しかし、そのは常にあなたも、私は多くの異なる方法で"HTTPエラー411を解決する方法。要求にはチャンクを入れたり、内容の長さを指定する必要があります。 in java

conn = (HttpURLConnection) new URL(url).openConnection(); 
conn.setRequestMethod(method); 
conn.setRequestProperty("X-DocuSign-Authentication", httpAuthHeader); 
conn.setRequestProperty("Accept", "application/json"); 
if (method.equalsIgnoreCase("POST")) { 
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    conn.setRequestProperty("Content-Length", Integer.toString(body.length())); 
      conn.setDoOutput(true); 
} 


status = conn.getResponseCode(); // triggers the request 
if (status != 200) { //// 200 = OK 
    errorParse(conn, status); 
    return; 
} 

InputStream is = conn.getInputStream(); 
+0

HttpClientを使用して作るのコードである。しかし、私はHttpClientをその作業を使用して同じことをやろう場合。 –

+0

HttpClient httpclient = new DefaultHttpClient(); –

+0

どのような方法を使用していますか?これはPUTに起こるはずです。 PUTをチェックするために 'if(method.equalsIgnoreCase(" POST "))'を変更すると、あなたの問題が解決するかもしれません。 –

答えて

0

HttpConnectからHttpClientに移動しました。そこで私はHttpURLConnectionから離れてhttp HttpClientオブジェクトを作成し、executeメソッドを呼び出してサーバーからデータを取得しました。以下は

は、httpリクエストではなく HttpURLConnection

try { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(authUrl); 
    String json = ""; 
    JSONObject jsonObject = new JSONObject(); 
    jsonObject.accumulate("phone", "phone"); 
    json = jsonObject.toString(); 
    StringEntity se = new StringEntity(json); 
    httpPost.setEntity(se); 

    httpPost.addHeader("Accept", "application/json"); 
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 

    HttpResponse httpResponse = httpclient.execute(httpPost); 

    // 9. receive response as inputStream 
    inputStream = httpResponse.getEntity().getContent(); 
    String response = getResponseBody(inputStream); 

    System.out.println(response); 

} catch (ClientProtocolException e) { 
    System.out.println("ClientProtocolException : " + e.getLocalizedMessage()); 
} catch (IOException e) { 
    System.out.println("IOException:" + e.getLocalizedMessage()); 
} catch (Exception e) { 
    System.out.println("Exception:" + e.getLocalizedMessage()); 
} 
0

をコンテンツの長さを設定しようとしたあなたは、コンテンツの長さを設定しませんが、決してしている設定やコンテンツの長さに問題がなかったと言ってリクエスト本体を送信します。

にコンテンツの長さを設定しないでください。 Javaはあなたのためにそれを行います。

NB setDoOutput(true)は、メソッドをPOSTに設定します。

+0

私もそれを試しましたが、うまくいきませんでした。実際には、私はdocusignサーバーに文字列を送り、そこからトークンを期待しています。私はこれを実装しようとしています:https://www.docusign.com/p/RESTAPIGuide/Content/OAuth2/OAuth2%20Token%20Request.htm –

+1

あなたは何を試しましたか? POSTをしている場合、何かを送る必要があります。あなたではない。あなたの 'HttpClient'コードはそうしています。それがその理由です。 – EJP

+0

それは私のために働かなかった。私の答えで投稿した他の方法は私のために働いた。それはHttpGet/HttpPostメソッドがdocusignとeOriginalのためにうまくいったように見えます –

関連する問題