httpclient开发

来源:互联网 发布:剑灵剑士捏脸数据大全 编辑:程序博客网 时间:2024/06/02 07:27

1)引入httpclient开发所需要的jar包

2)实例一:

package testHttpClient;

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

class fection
{
 static public void main(String []args) throws IOException
 {
  int i = 0;
  while(i<2)//做人要厚道
  {
         String PostURL=("http://sms.api.bz/fetion.php?username=&password=&sendto=&message=");
            HttpClient client = new HttpClient();
            PostMethod postMethod =  new PostMethod(PostURL);
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GBK");//发送中文,避免乱码
            postMethod.addParameter("username","13640815106");
            postMethod.addParameter("password","不告诉你");
            postMethod.addParameter("sendto","13640815106");//立方
            postMethod.addParameter("message","试试看,这是第"+Integer.toString(i+1)+"条");
            client.setConnectionTimeout(10000);
            client.executeMethod(postMethod);
            i++;
  }
        System.out.println("OK!");
 }


}

2)实例二:

package testHttpClient;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


public class Phone {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Phone ph=new Phone();
        System.out.println("phone");
        String adder=ph.getArea("13933600146");
        System.out.println(adder);
        System.out.println(adder.length());
    }
       /**
     * 使用GET方式提交数据
     * @param phone
     * @return
     */
      private static HttpMethod getGetMethod(String phone){
            return new GetMethod("/features/baibaosearch.jsp?query="+phone);
        }
      /**

         * 使用POST方式提交数据

         * @return
*/
 private static HttpMethod getPostMethod(String phone){
        PostMethod post = new PostMethod("/features/baibaosearch.jsp");
        NameValuePair simcard = new NameValuePair("query",phone);
        post.setRequestBody(new NameValuePair[] { simcard});       
        return post;
    }
     public String getArea(String phone){
            String result = "";
            try{
                HttpClient client = new HttpClient();
//                client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http"); 
                client.getHostConfiguration().setHost("www.sogou.com",80,"http"); 
                //      HttpMethod method = getPostMethod(phone);//使用POST方式提交数据
                 HttpMethod method = getPostMethod(phone);//使用POST方式提交数据
                client.executeMethod(method);
               //打印服务器返回的状态
                System.out.println(method.getStatusLine());
                //打印结果页面
                String response = method.getResponseBodyAsString();
//                String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
               //打印返回的信息
                int index = response.indexOf("手机号");
                response = response.substring(index,response.length());
                int index2 = response.indexOf("数据来源");
                response = response.substring(0, index2);
                result = response;
                method.releaseConnection();               
            }catch(Exception e){
                e.printStackTrace();
                result = "异常 : "+e.getMessage();
            }
            return result;
        }

}

 

原创粉丝点击