zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Ruby(1):入门
    html 制作静态页面新知识
    mysql 可视化界面操作指令
    html 基础
    Eclipse导入Java工程导入错误
    shell 25个常用命令
    java JDBC
    java 8新特性 instant
    git
    spring mvc 注解详解
  • 原文地址:https://www.cnblogs.com/HackHer/p/4994246.html
Copyright © 2011-2022 走看看