Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.json.other.client;
 
 import java.net.URL;
	   private URL url;
	    private final Map<StringStringheaders;
	    public HttpJsonRpcClientTransportOld(URL url) {
	        this. = url;
	        this. = new HashMap<StringString>();
	    }
	    public final void setHeader(String keyString value) {
	        this..put(keyvalue);
	    }
	    public final String call(String requestDatathrows Exception {
	        String responseData = post(requestData);
	        return responseData;
	    }
	    private String post(URL urlMap<StringStringheadersString data)
	            throws IOException {
	        HttpURLConnection connection = (HttpURLConnectionurl.openConnection();
	        if (headers != null) {
	            for (Map.Entry<StringStringentry : headers.entrySet()) {
	                connection.addRequestProperty(entry.getKey(), entry.getValue());
	            }
	        }
	        connection.addRequestProperty("Accept-Encoding""gzip");
	        connection.setRequestMethod("POST");
	        connection.setDoOutput(true);
	        connection.connect();
	        OutputStream out = null;
	        try {
	            out = connection.getOutputStream();
	            out.write(data.getBytes());
	            out.flush();
	            out.close();
	            int statusCode = connection.getResponseCode();
	            if (statusCode != .) {
	                throw new JsonRpcClientException("unexpected status code returned : " + statusCode);
	            }
	        } finally {
	            if (out != null) {
	                out.close();
	            }
	        }
	        String responseEncoding = connection.getHeaderField("Content-Encoding");
	        responseEncoding = (responseEncoding == null ? "" : responseEncoding.trim());
	        ByteArrayOutputStream bos = new ByteArrayOutputStream();
	        InputStream in = connection.getInputStream();
	        try {
	            in = connection.getInputStream();
	            if ("gzip".equalsIgnoreCase(responseEncoding)) {
	                in = new GZIPInputStream(in);
	            }
	            in = new BufferedInputStream(in);
	            
	            byte[] buff = new byte[1024];
	            int n;
	            while ((n = in.read(buff)) > 0) {
	                bos.write(buff, 0, n);
	            }
	            bos.flush();
	            bos.close();
	        } finally {
	            if (in != null) {
	                in.close();
	            }
	        }
	        return bos.toString();
	    }
New to GrepCode? Check out our FAQ X