我正在尝试通过我的 java 代码中的 HTTP POST 调用 servlet。下面是我的代码
private void sendRequest(String Url)
{
//Url contains all the POST parameters
try {
URL url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
但是请求不会发送到 servlet。我试图通过浏览器访问该 URL,它工作正常。是我遗漏了什么还是代码有任何问题。请帮忙。