post请求json字符串为参数(判断https和http)

SendPostUtil类:

public class SendPostUtil {

 
    public static String sendPosthttpclient(String url, String Params) throws Exception {
        //根据请求方式判断是https还是http请求
        if ("https".equals(url.substring(0, 5))) {
            org.apache.http.client.HttpClient httpClient = null;
            HttpPost httpPost = null;
            String result = null;
            try {
                httpClient = new SSLClient();
                httpPost = new HttpPost(url);
                StringEntity stringEntity = new StringEntity(Params,"utf-8");
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json; charset=utf-8");
                httpPost.setEntity(stringEntity);
                HttpResponse response = httpClient.execute(httpPost);
                if (response != null) {
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }
                }
            } catch (Exception ex) {
                throw new Exception("00", "连接系统超时");
            }
            return result;
        } else {
            DataOutputStream out = null;
            BufferedReader reader = null;
            StringBuffer response = new StringBuffer();
            try {
                URL httpUrl = null; // HTTP URL类 用这个类来创建连接
                // 创建URL
                httpUrl = new URL(url);
                // 建立连接
                HttpURLConnection conn = (HttpURLConnection) httpUrl
                        .openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "text/json");
                conn.setRequestProperty("connection", "keep-alive");
                conn.setUseCaches(false);// 设置不要缓存
                conn.setInstanceFollowRedirects(true);
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.connect();
                out = new DataOutputStream(conn.getOutputStream());
                out.write(Params.getBytes("utf-8"));
                // 读取响应
                reader = new BufferedReader(new InputStreamReader(
                        conn.getInputStream(), "UTF-8"));
                String lines;
                while ((lines = reader.readLine()) != null) {
                    response.append(lines);
                }
                reader.close();
                // 断开连接
                conn.disconnect();
            } catch (Exception e) {
                throw new Exception("00", "连接系统超时");
            }
            // 使用finally块来关闭输出流、输入流
            finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return response.toString();
        }
    }
}

2.SSLClient类:

public class SSLClient extends DefaultHttpClient {

	public SSLClient() throws Exception {
		super();
		//传输协议需要根据自己的判断
		SSLContext ctx = SSLContext.getInstance("TLS");
		X509TrustManager tm = new X509TrustManager() {
			@Override
			public void checkClientTrusted(X509Certificate[] chain,
										   String authType) throws CertificateException {
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain,
										   String authType) throws CertificateException {
			}

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};
		ctx.init(null, new TrustManager[]{tm}, null);
		SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		ClientConnectionManager ccm = this.getConnectionManager();
		SchemeRegistry sr = ccm.getSchemeRegistry();
		sr.register(new Scheme("https", 443, ssf));
	}
}
发布了68 篇原创文章 · 获赞 93 · 访问量 8511

猜你喜欢

转载自blog.csdn.net/qq_34707456/article/details/103353573