2009-12-16 34 views
-1

我想提出使用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

但是我總是從服務器獲得響應400,我也想知道如何在建立連接時設置用戶名和密碼?我正在使用setrequestproperty方法。 – sarah 2009-12-16 11:08:57

+0

和連接使用https,所以我設置端口和主機也 – sarah 2009-12-16 11:09:32

+0

如果連接是https,那麼你的URL是錯誤的,它說「http」。在URL上設置主機和端口是好的。你的用戶名和密碼是否在查詢字符串中?如: http://www.xyz.dom?user =用戶名和密碼=密碼 如果是這樣,那麼請求屬性不是你想要的。我想你只是把查詢字符串放在URL中就像我一樣。 – 2009-12-16 11:25:34

相關問題