【问题标题】:How to pass some parmeters with a HTTP request which is sent to Tomcat WebApp?如何通过发送到 Tomcat WebApp 的 HTTP 请求传递一些参数?
【发布时间】:2012-06-20 03:11:37
【问题描述】:

我想通过 HTTP 请求将这些参数传递给托管在 Tomcat 容器中的 WebApp, 用户名、密码、本次请求查询的资源、发送的是什么类型的请求

那么有没有办法将这些添加到 HTTP 标头?我听说我们可以使用 HTTP Basic 身份验证方式发送用户名和密码。有没有办法也发送其他参数?

【问题讨论】:

  • 将这些参数作为 HTTP get 或 POST 消息发送。
  • 不,我想在发送的 GET 或 Post 方法中添加一些值

标签: http tomcat servlets http-headers


【解决方案1】:

您可以使用具有身份验证详细信息的 Authorization 标头创建一个,以便进一步的请求加入会话并且不需要身份验证。

以下是添加授权标头的方法:

创建标头值。

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 );

【讨论】:

  • 添加标头的方法是什么,我应该使用什么类来发送 HTTP 请求。
  • 您使用哪个 API 来建立 http 连接。
  • 我不知道应该使用什么 API,但我已经使用此代码成功添加了一些标头。可以使用这个实现还是有其他好的方法可以做到这一点?字符串网页 = "10.100.3.83:9764/example/servlets/servlet/HelloWorldExample";网址网址 = 新网址(网页); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.addRequestProperty("名称","andunslg"); urlConnection.addRequestProperty("PW","admin");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多