码迷,mamicode.com
首页 > 编程语言 > 详细

Java HttpClient

时间:2015-11-25 13:27:12      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

1.读取网页内容

import java.io.Exception;

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.*;

publlic class SimpleClient{

public static void main(String[] args) throws IOException{

HttpClient client=new HttpClient();//创建HttpClient实例

///设置代理服务器地址和端口

//client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);

//选择提交方式  get/post请求页面

//使用get

HttpMethod method=new GetMethod("http://java.sun.com");

//使用post 

//HttpMethod method=new PostMethod("http://java.sun.com");

client.executeMethid(method);

//服务器返回状态

System.out.println(method.getStatusLine());

//返回信息

System.out.println(method.getResponseBodyAsString());

//释放链接

method.releaseConnection();

}

}

 

2. 以get  post 方式向网页提交参数,提交时设定页面的参数

get:

所有参数放到页面的url后面用问号与页面地址隔开,每个参数用&隔开

String geturl="http://java.sun.com/?name=  && mobile=";

HttpMethod method=new GetMethod(geturl);

post:

client.getHostConfiguration().setHost("www.***.com.cn",80,"http");

method=getPostMethod();

client.executeMethod(method);

System,out,println(method.getStatusLine());//打印结果页面

//返回信息

String response=new String(method.getResponseBodyAsString().getBytes("  "));

System,out.println(response);

//释放链接

method.releaseConnection();

 

使用get 提交数据

private static HttpMethodGet getGetMethod(){

return new GetMethod("/***.php?***=%%%% ");

}

使用post 提交参数

private ststic HttpMethod getPostMethod post=new PostMethod(){

PstMethod post=new PostMethod("/***.php");

NameValuePair ***=new NameValuePair("***","%%%%");

post.setRequestPair[](***));

reutrn post;

}

3.处理页面重定向

-----Http 协议的重定向机制:Http协议的3**都是Redirection

JSP/Servlet  response.sendRedirect 使用Http协议的重定向机制:

    返回状态码,客户端读取需要跳转到的页面的url并重新加载新的页面

JSP <jsp:forward page=" ">

    在服务器中实现页面的跳转,应用的容器加载了所要跳转的页面内容并返回给客户端

 

 

//////////详见http://www.blogjava.net/jelver/articles/162339.html

Java HttpClient

标签:

原文地址:http://www.cnblogs.com/HackHer/p/4994246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!