HttpClient 4.1.3 初学篇---使用Get和Post模拟登录简单页面(分别带参数)

    最近需要解决的问题需要用到Httpclient,模拟登陆网站!成功之后就可以用代码的方式获取网站内容或者发送请求,类似网络爬虫。

    但是在网上找了好多篇Blog,发现每一片的写法都不一样,也纠结了些时间,很纳闷,才发现Httpclient版本不一样。。。现在在这里说明我使用的版本是HttpClient 4.1.3,我已上传下载

    看了些Blog,发现直接访问大型的网站不太容易,于是就自己写了小的站点,只有一个Servlet,来接受参数(用户名和密码)就ok了!

这个Servlet就只有get与post方法,而且方法一样,如下

<span style="font-family:Microsoft YaHei;font-size:14px;">public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String psd = request.getParameter("pwd");
		if("wang".equals(name)&&"123".equals(psd)){
			out.print("true");
		}else{
			out.print("false");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		this.doGet(request, response);
	}
</span>
简单的“服务器”就搭建好了,接下来就是在客户端使用Httpclient模拟登录了!

先分析一下想要成功的登录进去的Url是:http://localhost:8080/HttpServer/LoginServlet?name=wang&pwd=123

参数就是两个("name","wang")和("pwd","123");

接下来就是HttpClent登场了!!!

  • 创建client

<span style="font-family:Microsoft YaHei;font-size:14px;">HttpClient client = new DefaultHttpClient();</span>

  • 选择发送请求的方式

<span style="font-family:Microsoft YaHei;font-size:14px;">HttpPost httpPost = new HttpPost(url);								 //发送请求的方法,此处为Post					
HttpGet httpGet = new HttpGet(url);                                                              //发送请求的方法,此处为Get</span>

  • 设置参数,Post和Get添加参数的方式不同,下面代码有给出
  • 发送请求

<span style="font-family:Microsoft YaHei;font-size:14px;">       HttpResponse response = client.execute(httpPost); </span>
服务器收到请求做出的响应和相关Html内容都在response里,就看怎么使用了!

下面是代码

<span style="font-family:Microsoft YaHei;font-size:14px;">private static void PostMethd() throws UnsupportedEncodingException,
	IOException, ClientProtocolException {
		HttpClient client = new DefaultHttpClient();                         //创建clent
		HttpPost httpPost = new HttpPost(url);								 //发送请求的方法,此处为Post					
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();     //Post方式添加参数的方法
		formparams.add(new BasicNameValuePair("name",name));
		formparams.add(new BasicNameValuePair("pwd", pwd));
		UrlEncodedFormEntity uefEntity;
		uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");           //防止出现乱码,加上UTF-8
		httpPost.setEntity(uefEntity);
		HttpResponse response = client.execute(httpPost);                    //提交请求
		InputStream in = PringStatus(response);                              
		in.close();                                                          //关闭输出流
		client.getConnectionManager().shutdown();                            //关闭client
	}

	

	private static void GetMethd() throws UnsupportedEncodingException,
	IOException, ClientProtocolException {
		HttpClient client = new DefaultHttpClient();                         
		StringBuilder sb = new StringBuilder(url);                           //此处是Get请求,添加参数的方法有点复杂,但我没有找到好的方法,
		sb.append("?");														 //还请看官拍砖
		sb.append("name=").append(name);
		sb.append("&&");
		sb.append("pwd=").append(pwd);
		
		HttpGet httpGet = new HttpGet(sb.toString());
		HttpResponse response = client.execute(httpGet);
		InputStream in = PringStatus(response);
		in.close();
		client.getConnectionManager().shutdown();
	}
	
	private static InputStream PringStatus(HttpResponse response)
			throws IOException, UnsupportedEncodingException {
		System.out.println(response.getStatusLine().toString());			  //请求发送之后,服务器响应的状态
		InputStream in = response.getEntity().getContent();
		BufferedReader br = new BufferedReader
				(new InputStreamReader(in,"utf-8"));                          
		String line = null;
		while((line = br.readLine())!=null){
			System.out.println(line.toString());
		}
		return in;
	}</span>
好,打开tomcat,跑起来验证一下是否成功!

<span style="font-family:Microsoft YaHei;font-size:14px;">HTTP/1.1 200 OK
true
</span>
第一个行是服务器响应的状态,第二行则是html内容,因为servlet里的是

<span style="font-family:Microsoft YaHei;font-size:14px;">out.print("true");</span>

到这里,HttpClient模拟登录简单的网站就完成了,接下来我会继续学习使用HttpClient登录网站,并保留cookie,发送请求并解析等相关内容!
代码下载



你可能感兴趣的:(java,网络,网络爬虫)