• Java HttpClient


    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

  • 相关阅读:
    EasyUI datagrid在insertRow时如果有formatter会出现EasyUI TypeError: rowData.assertEntity is undefined
    序列化与反序列化问题
    序列化和反序列化问题
    Java与.NET的WebServices相互调用
    嫁给程序员吧!!!
    五年之痒
    用户体验5大要素
    敏捷软件开发
    云计算风险识别
    Cute Editor for .NET v6.4
  • 原文地址:https://www.cnblogs.com/HackHer/p/4994246.html
Copyright © 2020-2023  润新知