怎么用java模拟浏览器提交html页面的表单数据
3个回答
2015-02-07 · 知道合伙人软件行家
关注
展开全部
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包
纯手工打字,请采纳哈
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("http://www.ip138.com/ips8.asp");
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是怎么样的了。
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("http://www.ip138.com/ips8.asp");
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是怎么样的了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-02-07
展开全部
使用组件httpclient,网上有下载。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询