java 模拟登陆 写给自己看
Posted 取个名字之麻烦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 模拟登陆 写给自己看相关的知识,希望对你有一定的参考价值。
package denglu; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.message.BasicNameValuePair; public class Tset { public void shiyan() throws ClientProtocolException, IOException{//获取页面信息 HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://192.168.33.160:8088/login.html"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); String html = EntityUtils.toString(entity, "GBK"); httpget.releaseConnection(); System.out.println(html); } public void login() throws ClientProtocolException, IOException{ HttpPost httppost = new HttpPost("http://192.168.33.160:8088/login.html"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name", "admin")); params.add(new BasicNameValuePair("password", "admin")); httppost.setEntity(new UrlEncodedFormEntity(params)); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); httppost.releaseConnection(); //验证是否登陆成功 如果失败是打不开登陆后的页面的 //String memberpage = "http://192.168.33.160:8088/hlslist.html"; String url="http://192.168.33.160:8088/api/addHLSList?n1=shiyan7&n2=rtsp://admin:[email protected]:554/1.sdp&n3=0"; HttpGet httpget = new HttpGet(url); response = httpclient.execute(httpget); // 必须是同一个HttpClient! HttpEntity entity = response.getEntity(); String html = EntityUtils.toString(entity, "GBK"); httpget.releaseConnection(); System.out.println(html); } public static void main(String[] args) throws ClientProtocolException, IOException{ Tset t=new Tset(); t.login(); } }
以上是关于java 模拟登陆 写给自己看的主要内容,如果未能解决你的问题,请参考以下文章