zoukankan      html  css  js  c++  java
  • httpclient post 请求

    package com.thinkgem.jeesite.common.utils;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.ParseException;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.scheme.Scheme;
    import org.apache.http.conn.ssl.SSLSocketFactory;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.codehaus.jettison.json.JSONObject;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.security.*;
    import java.security.cert.CertificateException;
    import java.util.*;
    
    public class HttpUtil {
        HttpClient httpclient=new DefaultHttpClient();
    
        /**
         * 发送 post请求访问本地应用并根据传递参数不同返回不同结果
         */
        public String post(String url,String reqEncoding,String respEncoding,JSONObject jsonObj) {
            String resStr = "";
            // 创建httppost
            HttpPost httppost = new HttpPost(
                    url);
            // 创建参数队列
            //List<NameValuePair> formparams = jsonObj;
            
            UrlEncodedFormEntity uefEntity;
            try {
                
                StringEntity sEntity = new StringEntity(jsonObj.toString(),"utf-8");//解决中文乱码问题 
                sEntity.setContentEncoding("UTF-8");
                sEntity.setContentType("application/json"); 
                
                //httppost.setEntity(uefEntity);
                httppost.setEntity(sEntity);
                HttpResponse response;
                response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    resStr = EntityUtils.toString(entity,respEncoding);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
               // httpclient.getConnectionManager().shutdown();
            }
            return resStr;
        }
    
        /**
         * 发送 get请求
         */
        public String get(String url) {
            String resStr = "";
            try {
                // 创建httpget.
                HttpGet httpget = new HttpGet(url);
                // 执行get请求.
                HttpResponse response = httpclient.execute(httpget);
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    
                    resStr=EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭连接,释放资源
                //httpclient.getConnectionManager().shutdown();
            }
            return resStr;
        }
        
        
        public static void main(String[] args) throws ClientProtocolException, IOException {
            /*HttpUtil ht = new HttpUtil();// 构造参数
            String requestTime = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
            requestTime = requestTime
                    .replaceFirst("" + requestTime.charAt(10), "T");//替换空为T
            
            requestTime += ".000";//增加毫秒数
        
            ApiAuthUtil apiAuthUtil = new ApiAuthUtil("InterFlight", "Query",
                    "FzSearch", "4874257a-c262-4586-bd4b-09b95a500857",
                    "47dc925ec0a2ec96", requestTime, 96870);
            String apiURL = apiAuthUtil
                    .getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch");
            System.out.println(apiURL);
            String result = ht.post(apiAuthUtil.getApiURL("http://tcopenapi.17usoft.com/flight/Inter/Query/FzSearch"), "utf-8");
            System.out.println("result:" + result);*/
               
        }
    }
     
  • 相关阅读:
    hitb2017 sentosa writeup
    linux下system函数的简单分析
    深入Linux内核架构-虚拟文件系统-脑图
    深入Linux内核架构-进程虚拟内存-脑图
    深入Linux内核架构-进程间通信
    BCTF 2017 babyuse
    深入Linux内核架构-内存管理-脑图
    深入Linux内核架构-进程管理和调度-脑图
    zctf-2017-pwn-sandbox
    Node.js学习(3)-用express改写留言本
  • 原文地址:https://www.cnblogs.com/SHMILYHP/p/5122032.html
Copyright © 2011-2022 走看看