搜索
热搜: 活动 交友 discuz
查看: 3184|回复: 4
收起左侧

Java https提交

[复制链接]
  • TA的每日心情
    开心
    2016-12-18 07:46
  • 签到天数: 28 天

    [LV.4]偶尔看看III

    发表于 2013-9-26 18:15:45 | 显示全部楼层 |阅读模式
    部分代码:POST提交 不解释直接上代码:
    [Java] 纯文本查看 复制代码
    private static final String METHOD_POST = "POST";
    	private static final String DEFAULT_CHARSET = "utf-8";
    	
    	public static String doPost(String url, String params, String charset, int connectTimeout, int readTimeout) throws Exception {
    		String ctype = "application/json;charset=" + charset;
    		byte[] content = {};
    		if(params != null){
    			content = params.getBytes(charset);
    		}
    		
    		return doPost(url, ctype, content, connectTimeout, readTimeout);
    	}
    	public static String doPost(String url, String ctype, byte[] content,int connectTimeout,int readTimeout) throws Exception {
    		HttpsURLConnection conn = null;
    		OutputStream out = null;
    		String rsp = null;
    		try {
    			try{
    				SSLContext ctx = SSLContext.getInstance("TLS");
    		        ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
    		        SSLContext.setDefault(ctx);
    
    				conn = getConnection(new URL(url), METHOD_POST, ctype);	
    				conn.setHostnameVerifier(new HostnameVerifier() {
    					@Override
    					public boolean verify(String hostname, SSLSession session) {
    						return true;
    					}
    				});
    				conn.setConnectTimeout(connectTimeout);
    				conn.setReadTimeout(readTimeout);
    			}catch(Exception e){
    				throw e;
    			}
    			try{
    				out = conn.getOutputStream();
    				out.write(content);
    				rsp = getResponseAsString(conn);
    			}catch(IOException e){
    				throw e;
    			}
    			
    		}finally {
    			if (out != null) {
    				out.close();
    			}
    			if (conn != null) {
    				conn.disconnect();
    			}
    		}
    		
    		return rsp;
    	}
    
        private static class DefaultTrustManager implements X509TrustManager {
    
            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
    
            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
    
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
    
        }
    	
    	private static HttpsURLConnection getConnection(URL url, String method, String ctype)
    			throws IOException {
    		HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    		conn.setRequestMethod(method);
    		conn.setDoInput(true);
    		conn.setDoOutput(true);
    		conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
    		conn.setRequestProperty("User-Agent", "stargate");
    		conn.setRequestProperty("Content-Type", ctype);
    		return conn;
    	}
    
    	protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
    		String charset = getResponseCharset(conn.getContentType());
    		InputStream es = conn.getErrorStream();
    		if (es == null) {
    			return getStreamAsString(conn.getInputStream(), charset);
    		} else {
    			String msg = getStreamAsString(es, charset);
    			if (StringUtils.isEmpty(msg)) {
    				throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
    			} else {
    				throw new IOException(msg);
    			}
    		}
    	}
    
    	private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
    			StringWriter writer = new StringWriter();
    
    			char[] chars = new char[256];
    			int count = 0;
    			while ((count = reader.read(chars)) > 0) {
    				writer.write(chars, 0, count);
    			}
    
    			return writer.toString();
    		} finally {
    			if (stream != null) {
    				stream.close();
    			}
    		}
    	}
    
    	private static String getResponseCharset(String ctype) {
    		String charset = DEFAULT_CHARSET;
    
    		if (!StringUtils.isEmpty(ctype)) {
    			String[] params = ctype.split(";");
    			for (String param : params) {
    				param = param.trim();
    				if (param.startsWith("charset")) {
    					String[] pair = param.split("=", 2);
    					if (pair.length == 2) {
    						if (!StringUtils.isEmpty(pair[1])) {
    							charset = pair[1].trim();
    						}
    					}
    					break;
    				}
    			}
    		}
    
    		return charset;
    	}

  • TA的每日心情

    2013-11-4 00:33
  • 签到天数: 36 天

    [LV.5]常住居民I

    发表于 2013-9-26 21:47:02 | 显示全部楼层
    沙发 我路过一下  

    点评

    对楼主可是莫大的鼓励啊。。。  详情 回复 发表于 2013-9-26 22:07
  • TA的每日心情
    奋斗
    2019-1-5 01:55
  • 签到天数: 138 天

    [LV.7]常住居民III

    发表于 2013-9-26 22:07:34 | 显示全部楼层

    :lol 对楼主可是莫大的鼓励啊。。。
  • TA的每日心情
    郁闷
    2014-12-12 12:58
  • 签到天数: 105 天

    [LV.6]常住居民II

    发表于 2013-9-28 13:35:42 | 显示全部楼层
    虽然有地方没懂、但是还是感谢这段代码。。。
  • TA的每日心情
    慵懒
    2016-5-5 10:38
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2016-5-5 10:45:09 | 显示全部楼层
    新手  来看看  论坛里面java的东西好少
    您需要登录后才可以回帖 登录 | 入住

    本版积分规则

    申请友链| Archiver| 手机版| 鱼·后花园

    GMT+8, 2024-11-21 17:32 , Processed in 0.034670 second(s), 22 queries , Redis On.

    Powered by Discuz! X3.4

    © 2005-2024 鱼·后花园

    快速回复 返回顶部 返回列表