zoukankan      html  css  js  c++  java
  • HttpClientDopost方法

    public static String loginCheck_POST_HttpClient(String name,String pass,String url){
      String result = "";
      
      HttpPost post = new HttpPost(url);//创建httpClient的post请求对象
      //设置请求参数
      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(params, 5*1000);
      HttpConnectionParams.setSoTimeout(params, 5*1000);
      //传值
      //创建List集合,存放向服务器传递的数据
      List<NameValuePair> reqData = new ArrayList<NameValuePair>();
      NameValuePair p1 = new BasicNameValuePair("username", name);
      NameValuePair p2 = new BasicNameValuePair("userpass", pass);
      reqData.add(p1);
      reqData.add(p2);
      
      try {
       HttpEntity entity = new UrlEncodedFormEntity(reqData, "utf-8");
       post.setEntity(entity);//给post请求对象,设置上传的数据
       HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象
       HttpResponse res = client.execute(post);//执行post请求,获得结果
       if(res.getStatusLine().getStatusCode() == 200){
        HttpEntity resEntity = res.getEntity();//获得相应结果,是一个HttpEntity对象
        result = EntityUtils.toString(resEntity, "utf-8");//转换为字符串
       }
      } catch (UnsupportedEncodingException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      } catch (ClientProtocolException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      return result;
      
     }
     public static String loginCheck_POST_HttpClient(String name,String pass,String url){
      String result = "";
      
      HttpPost post = new HttpPost(url);//创建httpClient的post请求对象
      //设置请求参数
      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(params, 5*1000);
      HttpConnectionParams.setSoTimeout(params, 5*1000);
      //传值
      //创建List集合,存放向服务器传递的数据
      List<NameValuePair> reqData = new ArrayList<NameValuePair>();
      NameValuePair p1 = new BasicNameValuePair("username", name);
      NameValuePair p2 = new BasicNameValuePair("userpass", pass);
      reqData.add(p1);
      reqData.add(p2);
      
      try {
       HttpEntity entity = new UrlEncodedFormEntity(reqData, "utf-8");
       post.setEntity(entity);//给post请求对象,设置上传的数据
       HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象
       HttpResponse res = client.execute(post);//执行post请求,获得结果
       if(res.getStatusLine().getStatusCode() == 200){
        HttpEntity resEntity = res.getEntity();//获得相应结果,是一个HttpEntity对象
        result = EntityUtils.toString(resEntity, "utf-8");//转换为字符串
       }
      } catch (UnsupportedEncodingException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      } catch (ClientProtocolException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
      return result;
      
     }
     

  • 相关阅读:
    印度出差之一
    印度出差疯狂的鸡翅
    GET和POST两种基本请求方法的区别
    对sleep和wait的理解
    Jquery 鼠标滑过时改变元素透明度的动画效果
    记忆里的科比
    New Features Summary Of Sybase® PowerDesigner® 15.0 For Windows[PD15新功能特征摘要]
    WF工作流接口规范
    Foxmail安装文件夹的整体转移(收集)
    c# vs2005 多线程中调用窗体控件 (摘)
  • 原文地址:https://www.cnblogs.com/3674-it/p/5108755.html
Copyright © 2011-2022 走看看