2016-03-23 18 views
6

Sto usando HttpConnect e sto cercando di ottenere qualche token dal server. Ma ogni volta che provo per ottenere la risposta, il suo dire sempre non è stato impostato o problema con la lunghezza del contenuto anche ho cercato di impostare la lunghezza del contenuto in molti modi diversiCome risolvere "Errore HTTP 411. La richiesta deve essere suddivisa in blocchi o avere una lunghezza del contenuto." 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

Ma se provo a fare la stessa cosa usando HttpClient suo funzionamento. –

+0

HttpClient httpclient = new DefaultHttpClient(); –

+0

Quali metodi stai utilizzando. Questo dovrebbe accadere per PUT. Cambiando 'if (method.equalsIgnoreCase (" POST "))' per verificare PUT potrebbe risolvere il tuo problema –

risposta

0

Lo spostamento da HttpConnect a HttpClient ha funzionato per me. Così mi sono allontanato da HttpURLConnection e creato un oggetto http HttpClient e richiamato i metodi di esecuzione per ottenere dati dal server.

Di seguito è il codice che fanno richiesta HTTP utilizzando HttpClient piuttosto 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

si sta impostando un contenuto di lunghezza, ma mai inviare un corpo di richiesta.

Non impostare la lunghezza del contenuto. Java lo fa per te.

NB setDoOutput(true) imposta il metodo su POST.

+0

Ho provato anche con quello ma non ha funzionato. In realtà sto inviando una stringa al server docusign e mi aspetto un token da loro. Sto cercando di implementare questo: https://www.docusign.com/p/RESTAPIGuide/Content/OAuth2/OAuth2%20Token%20Request.htm –

+1

Hai provato cosa? Se stai facendo un POST, devi inviare qualcosa. Tu non sei. Il tuo codice 'HttpClient' lo fa, ed è per questo che funziona. – EJP

+0

non ha funzionato per me. l'altro modo in cui ho postato la mia risposta ha funzionato per me. sembra che i metodi HttpGet/HttpPost funzionino bene per docusign e eOriginal –