• httppost的用法


    一,案例一

    定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型),这个代码多处用于Java像url发送Post请求。在发送post请求时用该list来存放参数。发送请求的大致过程如下:

     1 String url="http://www.baidu.com";
     2 HttpPost httppost=new HttpPost(url); //建立HttpPost对象
     3 List<NameValuePair> params=new ArrayList<NameValuePair>();
     4 //建立一个NameValuePair数组,用于存储欲传送的参数
     5 params.add(new BasicNameValuePair("pwd","2544"));
     6 //添加参数
     7 httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
     8 //设置编码
     9 HttpResponse response=new DefaultHttpClient().execute(httppost);
    10 //发送Post,并返回一个HttpResponse对象

    二,案例二

     1  /** 
     2      * 获得HttpPost对象 
     3      *  
     4      * @param url 
     5      *            请求地址 
     6      * @param params 
     7      *            请求参数 
     8      * @param encode 
     9      *            编码方式 
    10      * @return HttpPost对象 
    11      * @throws UnsupportedEncodingException 
    12      */  
    13     private static HttpPost getHttpPost(String url, Map<String, String> params,  
    14             String encode) throws UnsupportedEncodingException {  
    15         HttpPost httpPost = new HttpPost(url);  
    16         if (params != null) {  
    17             List<NameValuePair> form = new ArrayList<NameValuePair>();  
    18             for (String name : params.keySet()) {  
    19                 form.add(new BasicNameValuePair(name, params.get(name)));  
    20             }
    21             
    22             UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form,  
    23                     encode);  
    24             httpPost.setEntity(entity);  
    25         }
    26         
    27         return httpPost;  
    28     }  

    三,总结

    httpPost其实在服务端模拟浏览器向其它接口发送服务的,一般情况下和httpclient,或者jsonp联合使用,可以把它理解为浏览器就行了,里面封装了http协议的一些东西,所以要对http协议有一定的了解。

  • 相关阅读:
    if
    C#
    C#
    C#
    .net 5.0
    .net 5.0
    .net 5.0
    设计模式
    GAN网络中采用导向滤波的论文
    pytorch~多loss的选择
  • 原文地址:https://www.cnblogs.com/kdy11/p/9040602.html
Copyright © 2020-2023  润新知