2011-09-23 29 views
1

我想在java中作出休息請求。我使用Firefox中的RestClient測試了Web服務,它效果很好。設置方法不工作HttpsURLConnection

當我嘗試修改java中的HttpsUrlConnection實例的值不會改變,我得到一個500響應代碼。

這裏是我的代碼:

public String getAuthToken() throws IOException { 
    URL url =new URL("https://webserviceurl"); // webservice url is the url of the webservice 
    String data = URLEncoder.encode("username") + "=" + URLEncoder.encode("myusername","UTF-8"); 
    data+= "&" + URLEncoder.encode("password") + "=" + URLEncoder.encode("pass","UTF-8"); 
    HttpsURLConnection conn =(HttpsURLConnection) url.openConnection(); 
    conn.setUseCaches(false); 
    conn.setHostnameVerifier(new AllowAllHostnameVerifier()); //this works HostName verifier changes 
    conn.setRequestMethod("POST"); // this doens't work. requestMethod is still set to GET 
    conn.setDoOutput(true); // this doesnt work. DoOutput still set on false 
    conn.setRequestProperty("Content-Type", "application/json"); // doens't work either 
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(),"UTF-8"); 
    wr.write(data); 
    wr.flush(); 
    wr.close(); 
    //conn has a 500 response code 
    if (conn.getResponseCode()==200) 
    { 
    InputStream is = conn.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader rd = new BufferedReader(isr); 
    String token = rd.readLine(); 
    rd.close(); 
    return token; 
    } 
    else 
     return null; 

} 

我在這一點上stucked並不能發現什麼,使這項工作。

謝謝!

+0

你確定需要'POST'用戶名和密碼,而不是使用'GET',並將它們作爲參數在url中嗎? – ernazm

+0

這是一個內部服務器錯誤。此錯誤只能通過修復Web服務器軟件來解決。 **這不是客戶端問題**。 Web服務器站點的運營商應該定位和分析日誌,以提供有關錯誤的更多信息。 – mrkhrts

+0

好的,但實際的方法沒有爲HttpsURLConnection做任何事情。我已經將其更改爲HttpURLConnection並且它可以工作。 –

回答

0

我實際上認爲這是一個HttpsURLConnection的錯誤。當我將它改爲HttpURLConnection對象時,一切正常。

+0

我也有這個問題,我需要一個HttpsURLConnection> :( – NotNormal