我正在尝试在我的应用程序中使用 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();
谢谢你的回答。埃里克