怎么用java模拟浏览器提交html页面的表单数据

如题所述

HttpClient模拟请求如下
HttpClient  httpclient = new DefaultHttpClient();        //打开浏览器
HttpPost    httpPost = new HttpPost("www.xxx.xxx");    //输入网址

List <NameValuePair> nvps = new ArrayList<NameValuePair>();  
nvps.add(new BasicNameValuePair("userName","123"));   
nvps.add(new BasicNameValuePair("password","123"));   //封装表单
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); //将参数传入post方法中 
HttpResponse response = httpclient.execute(httpPost);    //执行post
HttpEntity   entity   = response.getEntity();    //获取响应数据
String result = EntityUtils.toString(entity);    //将响应数据转成字符串
需要导入jar包
纯手工打字,请采纳哈


温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-02-07
httpclient就行了,给你个取IP的例子好了

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class IPHelper {

public String getSourceText(String ip) throws IOException {
String text = null;
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
PostMethod post = new PostMethod("");
NameValuePair[] data = { new NameValuePair("action", "2"),
new NameValuePair("ip", ip) };
post.setRequestBody(data);
client.executeMethod(post);
text = post.getResponseBodyAsString();
post.releaseConnection();
return text;
}

public static void main(String[] args) throws IOException {
IPHelper h=new IPHelper();
System.out.println(h.getSourceText("192.169.0.1"));
}
}

这个是Post的,还有Get的,看你的form是怎么样的了。
第2个回答  2015-02-07
使用组件httpclient,网上有下载。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网