--sunfruit
用HttpURLConnection進行Post方式提交,下面給出一個例子
URL url = null;
HttpURLConnection httpurlconnection = null;
try
{
url = new URL("http://xxxx");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
String username="username=02000001";
httpurlconnection.getOutputStream().write(username.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(httpurlconnection!=null)
httpurlconnection.disconnect();
}
其中HttpURLConnection中的addRequestProperty方法,并不是用來添加Parameter 的,而是用來設置請求的頭信息,比如:
setRequestProperty("Content-type","text/html");
setRequestProperty("Connection", "close");
setRequestProperty("Content-Length",xxx);
當然如果不設置的話,可以走默認值,上面的例子中就沒有進行相關設置,但是也可以正確執行