我正在嘗試在我的應用程序中使用HttpURLConnection.我將我的請求方法設置為’GET’,但是當我嘗試檢索輸出流時,該方法將更改為’POST’!
我不確定是什么原因,但是當我使用’POST’發送請求時,我的
JSON服務器(我使用JAX-RS)會返回一個空白頁面.
這是我的代碼片段:
// Create the connection
HttpURLConnection con = (HttpURLConnection) new URL(getUrl() + uriP).openConnection();
// Add cookies if necessary
if (cookies != null) {
for (String cookie : cookies) {
con.addRequestProperty("Cookie",cookie);
Log.d("JSONServer","Added cookie: " + cookie);
}
}
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestMethod("GET");
con.setConnectTimeout(20000);
// Add 'Accept' property in header otherwise JAX-RS/CXF will answer a XML stream
con.addRequestProperty("Accept","application/json");
// Get the output stream
OutputStream os = con.getOutputStream();
// !!!!! HERE THE REQUEST METHOD HAS BEEN CHANGED !!!!!!
OutputStreamWriter wr = new OutputStreamWriter(os);
wr.write(requestP);
// Send the request
wr.flush();
謝謝你的回答.
埃里克