2012-06-18 34 views
1

我想在Tomcat容器託管的HTTP請求發送到Web應用程序, 用戶名,密碼,資源申請查詢,什麼類型的請求傳遞這些參數發送如何通過發送給Tomcat WebApp的HTTP請求傳遞一些參數?

那麼,有沒有辦法添加這些到HTTP頭?我聽說我們可以使用HTTP基本身份驗證方式發送用戶名和密碼。有沒有辦法發送其他參數呢?

+0

發送這些參數作爲HTTP GET或POST消息。 – Ankit

+0

否我想爲發送GET或Post方法添加一些值 – andunslg

回答

1

您可以使用具有驗證詳細信息的授權標頭,以便進一步的請求加入會話並且不需要驗證。

這裏是如何添加授權頭:

創建標頭值。

byte[] authBytes = Encoding.UTF8.GetBytes("user:password".ToCharArray()); 
String authHeaderValue = "Basic " + Convert.ToBase64String(authBytes); 

添加授權報頭與上述值

Authorization: authHeaderValue 

String webPage = "10.100.3.83:9764/example/servlets/servlet/…";; 
URL url = new URL(webPage); 
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();  
urlConnection.addRequestProperty("Name","andunslg"); 

//Username : andunslg 
//Password : admin 
byte[] authBytes = Encoding.UTF8.GetBytes("andunslg:admin".ToCharArray()); 
String authHeaderValue = "Basic " + Convert.ToBase64String(authBytes); 

urlConnection.addRequestProperty("Authorization",authHeaderValue); 
+0

添加標頭的方式以及我應該用來發送HTTP請求的類是什麼。 – andunslg

+0

您使用哪個API來建立http連接。 –

+0

我不知道應該使用哪個API,但是我已經成功地使用此代碼添加了一些頭文件。可以使用這個實現嗎?還是有其他的好辦法來做到這一點? String webPage =「http://10.100.3.83:9764/example/servlets/servlet/HelloWorldExample」; URL url =新的URL(網頁); HttpURLConnection urlConnection =(HttpURLConnection)url.openConnection(); urlConnection.addRequestProperty(「Name」,「andunslg」); urlConnection.addRequestProperty(「PW」,「admin」); – andunslg

相關問題