Java跨系統調用接口(POST)


package com.bing.util;


import com.bing.constant.ResultModel;
import com.bing.model.Company;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ss {
    public static String sendPost(String url, final Map<String,String> head, final Company paemear) throws Exception{
        boolean isSuccess = false;
        String str="";
        HttpPost post = null;
        HttpClient httpClient = new DefaultHttpClient();
        // 設置超時時間
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);

        post = new HttpPost(url);
        // 構造消息頭

        post.setHeader("Content-type", "application/json; charset=utf-8");
        for (Map.Entry<String,String> entry : head.entrySet()) {
            post.setHeader(entry.getKey(),entry.getValue());
        }
        // 構建消息實體
        // 發送Json格式的數據請求
        System.out.println(System.currentTimeMillis());
        HttpResponse response = httpClient.execute(post);
        System.out.println(System.currentTimeMillis());
        // 檢驗返回碼
        int statusCode = response.getStatusLine().getStatusCode();
        if(statusCode != HttpStatus.SC_OK){
            isSuccess = false;
        }else{
            int retCode = 0;
            /**讀取服務器返回過來的json字符串數據**/
            str = EntityUtils.toString(response.getEntity());
        }
        if(post != null){
            try {
                post.releaseConnection();
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    /**
     * 並發量測試
     * @param xian 線程池個數
     * @param count 每個線程的調用次數
     * @param url 調用的 URL
     * @param head 頭參數
     * @param paemear 傳入參數
     */
    public static void bing(final int xian,final int count, final String url, final Map<String,String> head, final Company paemear){

        for(int i = 1; i<xian; i++){
            final int m =i;
            Thread t = new Thread(new Runnable(){//線程啟動
                public void run(){
                    try {
                        int j = 0;//成功次數
                        int k = 0; //失敗次數
                        for (int l=1;l<count;l++){
                            System.out.println(m +"號線程");
                            String str = ss.sendPost(url,head,paemear);
                            System.out.println(str);
                            JSONObject jsonObject=JSONObject.fromObject(str);
                            ResultModel resultModel=(ResultModel)JSONObject.toBean(jsonObject, ResultModel.class);
                            if(resultModel.getError()==200){
                                j++;
                            }else{
                                k++;
                            }
                            System.out.println(m+"號線程正在調用:"+l+"次");
                        }
                        System.out.println(m+"號線程共調應100次 成功:"+j+"失敗:"+k);
                    }catch (Exception e){
                        System.out.println(m +"號線程查詢出錯");
                    }
                }
            });
            t.start();
        }
    }


    public static void main(String[] args){
        final Map<String,String> head = new HashMap<>();
        head.put("token","e7dd5368743457c51405802283d54057ca95f9c73a19fe54");
        head.put("terminalType","Android");
        Company paemear = new Company();
        bing(2,2,"http://192.168.2.203:8080/HouseLizardCloud/Company/addCompany",head,paemear);

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM