0
我用java 1.7在Tomcat 7, 我可以用這個代碼發送POST請求:的Java:打開與頭一個新的窗口和POST參數
private void processSelectProductOnChange(ActionEvent e) {
URL obj;
HttpURLConnection con;
String urlParameters = "";
String url = "http://myservice.example.com/Service/New";
String userAgent = request.getHeader("User-Agent");
int accountId = 5;
try {
obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", userAgent);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("ServiceName", "myServiceName");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
String json = getJSONObject(accountId);
wr.writeUTF(json);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
private String getJSONObject(int accountId) throws UnsupportedEncodingException
{
JSONObject jobj = new JSONObject();
jobj.put("AccountId", String.valueOf(accountId));
jobj.put("ServiceId", String.valueOf(2));
jobj.put("ExpirationDate", "");
jobj.put("Description", "");
return jobj.toString();
}
現在,而不是發送POST請求,我想要從我的Web應用程序打開一個新的瀏覽器窗口到URL,我該怎麼做?
編輯:我忘了說我想在新的瀏覽器Tab打開時執行POST請求。
這將做一個GET雖然 –