1. 程式人生 > >HttpClient傳送post請求

HttpClient傳送post請求

 /*HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httppost = new HttpPost("https:baidu.com");
        httppost.addHeader("charset", "utf-8");
        httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0");
        try {
            // 建立引數佇列  
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            formparams.add(new BasicNameValuePair("reportDate", DateFunction.getStrDateFormat("yyyy-MM-dd HH:mm:ss", smf.getSrCreateDate())));
            formparams.add(new BasicNameValuePair("titel", smf.getSrTitle()));
            formparams.add(new BasicNameValuePair("link", smf.getSrWebUrl()));
            formparams.add(new BasicNameValuePair("typeid", "1"));
            formparams.add(new BasicNameValuePair("content", smf.getSrDescription()));
            formparams.add(new BasicNameValuePair("msg", "false"));
            formparams.add(new BasicNameValuePair("key", "5fgpA6Pu8c"));
            formparams.add(new BasicNameValuePair("remark", "系統上報"));
            formparams.add(new BasicNameValuePair("imgdata", ""));
            formparams.add(new BasicNameValuePair("tablename", ""));
            formparams.add(new BasicNameValuePair("filename", ""));
            formparams.add(new BasicNameValuePair("extensions", ""));

            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(entity);
            HttpResponse httpResponse;

            httpResponse = closeableHttpClient.execute(httppost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = "";
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8").replace("\"{", "{").replace("}\"", "}").replaceAll("\\\\", "");
            }
            closeableHttpClient.close();*/