Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static ServerResponse getServerStream(String url, HashMap<String, String> parameter, String reqtype) {
- HttpURLConnection connection;
- ServerResponse streamBean = new ServerResponse();
- try {
- connection = (HttpURLConnection) (new URL(url)).openConnection();
- connection.setInstanceFollowRedirects(true);
- connection.setUseCaches(true);
- // connection.setDefaultUseCaches(true);
- connection.setReadTimeout(10000);
- connection.setConnectTimeout(15000);
- connection.setRequestMethod(reqtype);
- // connection.addRequestProperty("Cache-Control", "only-if-cached");
- connection.addRequestProperty("Cache-Control", "max-age=" + 30 * 60);
- int maxStale = 60 * 60 * 24 * 28; // tolerate 1 day stale
- connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
- if (reqtype == NetworkLoader.POST) {
- connection.setDoInput(true);
- connection.setDoOutput(true);
- if (parameter != null) {
- OutputStream os = connection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
- writer.write(getPostDataString(parameter));
- writer.flush();
- writer.close();
- os.close();
- }
- }
- connection.connect();
- System.out.println("Response Cache is installed : " + HttpResponseCache.getInstalled().getHitCount()+" Size: "+ HttpResponseCache.getInstalled().maxSize());
- System.out.println("Cached response is : " + HttpResponseCache.getInstalled().get(new URI(url.toString()), reqtype, connection.getHeaderFields()));
- //System.out.println("Connection Error: " + connection.getResponseCode());
- if (connection.getResponseCode() == 200 && connection.getInputStream() != null) {
- streamBean.setResponseCode(connection.getResponseCode());
- streamBean.setServerResponse(convertStreamToString(connection.getInputStream()));
- return streamBean;
- } else {
- streamBean.setResponseCode(connection.getResponseCode());
- streamBean.setServerResponse(convertStreamToString(connection.getErrorStream()));
- return streamBean;
- }
- } catch (EOFException eof) {
- streamBean.setException(eof);
- eof.printStackTrace();
- } catch (SocketException se) {
- streamBean.setException(se);
- se.printStackTrace();
- } catch (IOException exception) {
- streamBean.setException(exception);
- exception.printStackTrace();
- } catch (Exception exception) {
- streamBean.setException(exception);
- exception.printStackTrace();
- }
- return streamBean;
- }
Add Comment
Please, Sign In to add comment