java程序中怎样可以知道从页面提交的是用post还是get
展开全部
pclient方便,效率又高,下面是post方式提交登录参数的代码:
public class FormLoginDemo
{
static final String LOGON_SITE = "developer.java.sun.com";
static final int LOGON_PORT = 80;
public FormLoginDemo() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
PostMethod authpost = new PostMethod("/servlet/SessionServlet");
// 准备登录参数
NameValuePair action = new NameValuePair("action", "login");
NameValuePair url = new NameValuePair("url", "/index.html");
NameValuePair userid = new NameValuePair("UserId", "userid");
NameValuePair password = new NameValuePair("Password", "password");
authpost.setRequestBody(
new NameValuePair[] );
// 执行Post请求
client.executeMethod(authpost);
// 打印状态码
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// 释放连接
authpost.releaseConnection();
}
}
public class FormLoginDemo
{
static final String LOGON_SITE = "developer.java.sun.com";
static final int LOGON_PORT = 80;
public FormLoginDemo() {
super();
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
PostMethod authpost = new PostMethod("/servlet/SessionServlet");
// 准备登录参数
NameValuePair action = new NameValuePair("action", "login");
NameValuePair url = new NameValuePair("url", "/index.html");
NameValuePair userid = new NameValuePair("UserId", "userid");
NameValuePair password = new NameValuePair("Password", "password");
authpost.setRequestBody(
new NameValuePair[] );
// 执行Post请求
client.executeMethod(authpost);
// 打印状态码
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// 释放连接
authpost.releaseConnection();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询