如何在进行http连接时设置请求属性?

时间:2009-12-16 09:37:25

标签: java http

我正在使用

进行http连接
URL urlToConnect = new URL ("http://www.xyz.com");
HttpURLConnection connection = ( HttpURLConnection )  urlToConnect.openConnection();

我需要设置以下属性,我就像

一样
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("user-agent", USER_AGENT);
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("http_code", "200");
connection.setRequestProperty("USERNAME", "test");
connection.setRequestProperty("PASSWORD", "test");

这是设置所有这些数据的正确方法吗?

1 个答案:

答案 0 :(得分:1)

是的,你在做什么是完美的。