Example usage for java.net URLConnection setRequestProperty

List of usage examples for java.net URLConnection setRequestProperty

Introduction

In this page you can find the example usage for java.net URLConnection setRequestProperty.

Prototype

public void setRequestProperty(String key, String value) 

Source Link

Document

Sets the general request property.

Usage

From source file:com.cisco.dvbu.ps.deploytool.services.RegressionManagerUtils.java

/**
 * //from   ww w . j a v a 2  s  .  c  om
 * also @see com.compositesw.ps.deploytool.dao.RegressionPubTestDAO#executeWs(com.compositesw.ps.deploytool.dao.RegressionPubTestDAO.Item, String, String)
 */
public static int executeWs(RegressionItem item, String outputFile, CompositeServer cisServerConfig,
        RegressionTestType regressionConfig, String delimiter, String printOutputType)
        throws CompositeException {
    // Set the command and action name
    String command = "executeWs";
    String actionName = "REGRESSION_TEST";

    // Check the input parameter values:
    if (cisServerConfig == null || regressionConfig == null) {
        throw new CompositeException(
                "XML Configuration objects are not initialized when trying to run Regression test.");
    }

    URLConnection urlConn = null;
    BufferedReader rd = null;
    OutputStreamWriter wr = null;
    int rows = 0;
    String host = cisServerConfig.getHostname();
    int wsPort = cisServerConfig.getPort(); // port in servers.xml defines WS port
    boolean useHttps = cisServerConfig.isUseHttps();

    // Execute the webservice
    try {
        // Don't execute if -noop (NO_OPERATION) has been set otherwise execute under normal operation.
        if (CommonUtils.isExecOperation()) {
            boolean encrypt = item.encrypt;
            // Override the encrypt flag when useHttps is set from an overall PDTool over SSL (https) setting.
            if (useHttps && !encrypt) {
                encrypt = true;
                RegressionManagerUtils.printOutputStr(printOutputType, "summary",
                        "The regression input file encrypt=false has been overridden by useHttps=true for path="
                                + item.path,
                        "");
            }

            String urlString = "http://" + host + ":" + wsPort + item.path;
            if (encrypt) {
                urlString = "https://" + host + ":" + (wsPort + 2) + item.path;
            }
            RegressionManagerUtils.printOutputStr(printOutputType, "summary", "urlString=" + urlString, "");
            URL url = new URL(urlString);
            urlConn = url.openConnection();
            if (encrypt) {
                // disable hostname verification
                ((HttpsURLConnection) urlConn).setHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String urlHostName, SSLSession session) {
                        return true;
                    }
                });

            }
            // 2014-02-09 (mtinius) - added basic authorization to allow for connections with new users
            String credentials = cisServerConfig.getUser() + ":"
                    + CommonUtils.decrypt(cisServerConfig.getPassword());
            String encoded = Base64EncodeDecode.encodeString(credentials);
            urlConn.setRequestProperty("Authorization", "Basic " + encoded);

            urlConn.setRequestProperty("SOAPAction", item.action);
            urlConn.setRequestProperty("Content-Type", item.contentType);
            urlConn.setDoOutput(true);

            wr = new OutputStreamWriter(urlConn.getOutputStream());
            wr.write(item.input);
            wr.flush();

            // Get the response
            rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String line;
            StringBuffer buf = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                rows++;
                buf.append(line);
                if (outputFile != null)
                    CommonUtils.appendContentToFile(outputFile, line);
            }
            line = buf.toString();
            RegressionManagerUtils.printOutputStr(printOutputType, "results", line, "");
            if (line.indexOf("<fault") >= 0 || line.indexOf(":fault") >= 0) {
                if (rd != null) {
                    rd.close();
                }
                if (wr != null) {
                    wr.close();
                }
                throw new IllegalStateException("Fault encountered.");
            }
            if (line.trim().length() == 0) {
                if (rd != null) {
                    rd.close();
                }
                if (wr != null) {
                    wr.close();
                }
                throw new IllegalStateException("No response document.");
            }
            urlConn.getInputStream().close();
            //              urlConn.getOutputStream().flush();
            wr.close();
            rd.close();
            RegressionManagerUtils.printOutputStr(printOutputType, "results", "\nCompleted executeWs()", "");
        } else {
            logger.info("\n\nWARNING - NO_OPERATION: COMMAND [" + command + "], ACTION [" + actionName
                    + "] WAS NOT PERFORMED.\n");
        }

        return rows;
    } catch (IOException e) {
        try {
            HttpURLConnection httpConn = (HttpURLConnection) urlConn;
            BufferedReader brd = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
            String line;
            StringBuffer buf = new StringBuffer();
            while ((line = brd.readLine()) != null) {
                buf.append(line + "\n");
            }
            brd.close();
            String error = buf.toString();
            throw new ApplicationException("executeWs(): " + error, e);

        } catch (Exception err) {
            String error = e.getMessage() + "\n" + "DETAILED_MESSAGE=[" + err.getMessage() + "]";
            //debug:               System.out.println("*************** ERROR ENCOUNTERED IN executeWs THREAD FOR TYPE:webservice *****************");
            throw new ApplicationException("executeWs(): " + error, err);
        }
    } finally {
        try {
            if (rd != null) {
                rd.close();
            }
            if (wr != null) {
                wr.close();
            }
        } catch (Exception e) {
            rd = null;
            wr = null;
            throw new CompositeException(
                    "executeWs(): unable to close BufferedReader (rd) and OutputStreamWriter (wr): "
                            + e.getMessage());
        }
    }
}

From source file:com.adobe.aem.demomachine.communities.Loader.java

private static void postAnalytics(String analytics, String body) {

    if (analytics != null && body != null) {

        URLConnection urlConn = null;
        DataOutputStream printout = null;
        BufferedReader input = null;
        String tmp = null;/* w ww .  j ava 2s.c  o m*/
        try {

            logger.debug("New Analytics Event: " + body);

            URL sitecaturl = new URL("http://" + analytics);

            urlConn = sitecaturl.openConnection();
            urlConn.setDoInput(true);
            urlConn.setDoOutput(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            printout = new DataOutputStream(urlConn.getOutputStream());

            printout.writeBytes(body);
            printout.flush();
            printout.close();

            input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

            while (null != ((tmp = input.readLine()))) {
                logger.debug(tmp);
            }
            printout.close();
            input.close();

        } catch (Exception ex) {

            logger.warn("Connectivity error: " + ex.getMessage());

        } finally {

            try {
                input.close();
                printout.close();
            } catch (Exception e) {
                // Omitted
            }

        }

    }

}

From source file:com.jp.miaulavirtual.DisplayMessageActivity.java

public int downloadFile(String request) {
    URL url2;//w w  w . java2 s . c o m
    URLConnection conn;
    int lastSlash;
    Long fileSize = null;
    BufferedInputStream inStream;
    BufferedOutputStream outStream;
    FileOutputStream fileStream;
    String cookies = cookieFormat(scookie); // format cookie for URL setRequestProperty
    final int BUFFER_SIZE = 23 * 1024;
    int id = 1;

    File file = null;

    Log.d("Document", "2 respueesta");
    try {
        // Just resources
        lastSlash = url.toString().lastIndexOf('/');

        // Directory creation
        String root = Environment.getExternalStorageDirectory().toString();
        Boolean isSDPresent = android.os.Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);
        // check if is there external storage
        if (!isSDPresent) {
            task_status = false;
            id = 9;
        } else {
            String folder;
            if (comunidades) {
                folder = onData.get(2)[1];
            } else {
                folder = onData.get(1)[1];
            }
            folder = folder.replaceAll(
                    "\\d{4}-\\d{4}\\s|\\d{4}-\\d{2}\\s|Documentos\\sde\\s?|Gr\\..+?\\s|\\(.+?\\)", "");
            folder = folder.toString().trim();
            Log.d("Folder", folder);
            File myDir = new File(root + "/Android/data/com.jp.miaulavirtual/files/" + folder);
            myDir.mkdirs();

            // Document creation
            String name = url.toString().substring(lastSlash + 1);
            file = new File(myDir, name);
            Log.d("Document", name);
            fileSize = (long) file.length();

            // Check if we have already downloaded the whole file
            if (file.exists()) {
                dialog.setProgress(100); // full progress if file already donwloaded
            } else {
                // Start the connection with COOKIES (we already verified that the cookies aren't expired and we can use them)
                url2 = new URL(request);
                conn = url2.openConnection();
                conn.setUseCaches(false);
                conn.setRequestProperty("Cookie", cookies);
                conn.setConnectTimeout(10 * 1000);
                conn.setReadTimeout(20 * 1000);
                fileSize = (long) conn.getContentLength();

                // Check if we have necesary space
                if (fileSize >= myDir.getUsableSpace()) {
                    task_status = false;
                    id = 2;
                } else {

                    // Start downloading
                    inStream = new BufferedInputStream(conn.getInputStream());
                    fileStream = new FileOutputStream(file);
                    outStream = new BufferedOutputStream(fileStream, BUFFER_SIZE);
                    byte[] data = new byte[BUFFER_SIZE];
                    int bytesRead = 0;
                    int setMax = (conn.getContentLength() / 1024);
                    dialog.setMax(setMax);

                    while (task_status && (bytesRead = inStream.read(data, 0, data.length)) >= 0) {
                        outStream.write(data, 0, bytesRead);
                        // update progress bar
                        dialog.incrementProgressBy((int) (bytesRead / 1024));
                    }

                    // Close stream
                    outStream.close();
                    fileStream.close();
                    inStream.close();

                    // Delete file if Cancel button
                    if (!task_status) {
                        file.delete();
                        if (myDir.listFiles().length <= 0)
                            myDir.delete();
                        id = 0;
                    }
                }
            }
            Log.d("Status", String.valueOf(task_status));
            // Open file
            if (task_status) {
                Log.d("Type", "Hola2");
                dialog.dismiss();
                Intent intent = new Intent();
                intent.setAction(android.content.Intent.ACTION_VIEW);

                MimeTypeMap mime = MimeTypeMap.getSingleton();
                // Get extension file
                String file_s = file.toString();
                String extension = "";

                int i = file_s.lastIndexOf('.');
                int p = Math.max(file_s.lastIndexOf('/'), file_s.lastIndexOf('\\'));

                if (i > p) {
                    extension = file_s.substring(i + 1);
                }

                // Get extension reference
                String doc_type = mime.getMimeTypeFromExtension(extension);

                Log.d("Type", extension);

                intent.setDataAndType(Uri.fromFile(file), doc_type);
                startActivity(intent);
            }
        }
    } catch (MalformedURLException e) // Invalid URL
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 3;
    } catch (FileNotFoundException e) // FIle not found
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 4;
    } catch (SocketTimeoutException e) // time out
    {
        Log.d("Timeout", "Timeout");
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 7;
    } catch (Exception e) // General error
    {
        task_status = false;
        if (file.exists()) {
            file.delete();
        }
        id = 8;
    }
    Log.d("Type", String.valueOf(id));
    Log.d("StartOk3", "Como he llegado hasta aqu?");
    // notify completion
    Log.d("ID", String.valueOf(id));
    return id;

}

From source file:com.arm.connector.bridge.transport.HttpTransport.java

@SuppressWarnings("empty-statement")
private String doHTTP(String verb, String url_str, String username, String password, String data,
        String content_type, String auth_domain, boolean doInput, boolean doOutput, boolean doSSL,
        boolean use_api_token, String api_token) {
    String result = "";
    String line = "";
    URLConnection connection = null;
    SSLContext sc = null;//from  ww  w.  ja v  a 2s .  co  m

    try {
        URL url = new URL(url_str);

        // Http Connection and verb
        if (doSSL) {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            // Install the all-trusting trust manager
            try {
                sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                // do nothing
                ;
            }

            // open the SSL connction
            connection = (HttpsURLConnection) (url.openConnection());
            ((HttpsURLConnection) connection).setRequestMethod(verb);
            ((HttpsURLConnection) connection).setSSLSocketFactory(sc.getSocketFactory());
            ((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } else {
            connection = (HttpURLConnection) (url.openConnection());
            ((HttpURLConnection) connection).setRequestMethod(verb);
        }

        connection.setDoInput(doInput);
        if (doOutput && data != null && data.length() > 0) {
            connection.setDoOutput(doOutput);
        } else {
            connection.setDoOutput(false);
        }

        // enable basic auth if requested
        if (use_api_token == false && username != null && username.length() > 0 && password != null
                && password.length() > 0) {
            String encoding = Base64.encodeBase64String((username + ":" + password).getBytes());
            connection.setRequestProperty("Authorization", this.m_basic_auth_qualifier + " " + encoding);
            //this.errorLogger().info("Basic Authorization: " + username + ":" + password + ": " + encoding);
        }

        // enable ApiTokenAuth auth if requested
        if (use_api_token == true && api_token != null && api_token.length() > 0) {
            // use qualification for the authorization header...
            connection.setRequestProperty("Authorization", this.m_auth_qualifier + " " + api_token);
            //this.errorLogger().info("ApiTokenAuth Authorization: " + api_token);

            // Always reset to the established default
            this.resetAuthorizationQualifier();
        }

        // ETag support if requested
        if (this.m_etag_value != null && this.m_etag_value.length() > 0) {
            // set the ETag header value
            connection.setRequestProperty("ETag", this.m_etag_value);
            //this.errorLogger().info("ETag Value: " + this.m_etag_value);

            // Always reset to the established default
            this.resetETagValue();
        }

        // If-Match support if requested
        if (this.m_if_match_header_value != null && this.m_if_match_header_value.length() > 0) {
            // set the If-Match header value
            connection.setRequestProperty("If-Match", this.m_if_match_header_value);
            //this.errorLogger().info("If-Match Value: " + this.m_if_match_header_value);

            // Always reset to the established default
            this.resetIfMatchValue();
        }

        // specify content type if requested
        if (content_type != null && content_type.length() > 0) {
            connection.setRequestProperty("Content-Type", content_type);
            connection.setRequestProperty("Accept", "*/*");
        }

        // add Connection: keep-alive (does not work...)
        //connection.setRequestProperty("Connection", "keep-alive");

        // special gorp for HTTP DELETE
        if (verb != null && verb.equalsIgnoreCase("delete")) {
            connection.setRequestProperty("Access-Control-Allow-Methods", "OPTIONS, DELETE");
        }

        // specify domain if requested
        if (auth_domain != null && auth_domain.length() > 0) {
            connection.setRequestProperty("Domain", auth_domain);
        }

        // DEBUG dump the headers
        //if (doSSL) 
        //    this.errorLogger().info("HTTP: Headers: " + ((HttpsURLConnection)connection).getRequestProperties()); 
        //else
        //    this.errorLogger().info("HTTP: Headers: " + ((HttpURLConnection)connection).getRequestProperties()); 

        // specify data if requested - assumes it properly escaped if necessary
        if (doOutput && data != null && data.length() > 0) {
            try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream())) {
                out.write(data);
            }
        }

        // setup the output if requested
        if (doInput) {
            try {
                try (InputStream content = (InputStream) connection.getInputStream();
                        BufferedReader in = new BufferedReader(new InputStreamReader(content))) {
                    while ((line = in.readLine()) != null) {
                        result += line;
                    }
                }
            } catch (java.io.FileNotFoundException ex) {
                this.errorLogger().info("HTTP(" + verb + ") empty response (OK).");
                result = "";
            }
        } else {
            // no result expected
            result = "";
        }

        // save off the HTTP response code...
        if (doSSL)
            this.saveResponseCode(((HttpsURLConnection) connection).getResponseCode());
        else
            this.saveResponseCode(((HttpURLConnection) connection).getResponseCode());

        // DEBUG
        //if (doSSL)
        //    this.errorLogger().info("HTTP(" + verb +") URL: " + url_str + " Data: " + data + " Response code: " + ((HttpsURLConnection)connection).getResponseCode());
        //else
        //    this.errorLogger().info("HTTP(" + verb +") URL: " + url_str + " Data: " + data + " Response code: " + ((HttpURLConnection)connection).getResponseCode());
    } catch (IOException ex) {
        this.errorLogger().warning("Caught Exception in doHTTP(" + verb + "): " + ex.getMessage());
        result = null;
    }

    // return the result
    return result;
}

From source file:org.openqa.selenium.server.ProxyHandler.java

protected long proxyPlainTextRequest(URL url, String pathInContext, String pathParams, HttpRequest request,
        HttpResponse response) throws IOException {
    CaptureNetworkTrafficCommand.Entry entry = new CaptureNetworkTrafficCommand.Entry(request.getMethod(),
            url.toString());/*  w  ww.  j  a v  a2s  .co m*/
    entry.addRequestHeaders(request);

    if (log.isDebugEnabled())
        log.debug("PROXY URL=" + url);

    URLConnection connection = url.openConnection();
    connection.setAllowUserInteraction(false);

    if (proxyInjectionMode) {
        adjustRequestForProxyInjection(request, connection);
    }

    // Set method
    HttpURLConnection http = null;
    if (connection instanceof HttpURLConnection) {
        http = (HttpURLConnection) connection;
        http.setRequestMethod(request.getMethod());
        http.setInstanceFollowRedirects(false);
        if (trustAllSSLCertificates && connection instanceof HttpsURLConnection) {
            TrustEverythingSSLTrustManager.trustAllSSLCertificates((HttpsURLConnection) connection);
        }
    }

    // check connection header
    String connectionHdr = request.getField(HttpFields.__Connection);
    if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive)
            || connectionHdr.equalsIgnoreCase(HttpFields.__Close)))
        connectionHdr = null;

    // copy headers
    boolean xForwardedFor = false;
    boolean isGet = "GET".equals(request.getMethod());
    boolean hasContent = false;
    Enumeration enm = request.getFieldNames();
    while (enm.hasMoreElements()) {
        // TODO could be better than this!
        String hdr = (String) enm.nextElement();

        if (_DontProxyHeaders.containsKey(hdr) || !_chained && _ProxyAuthHeaders.containsKey(hdr))
            continue;
        if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0)
            continue;

        if (!isGet && HttpFields.__ContentType.equals(hdr))
            hasContent = true;

        Enumeration vals = request.getFieldValues(hdr);
        while (vals.hasMoreElements()) {
            String val = (String) vals.nextElement();
            if (val != null) {
                // don't proxy Referer headers if the referer is Selenium!
                if ("Referer".equals(hdr) && (-1 != val.indexOf("/selenium-server/"))) {
                    continue;
                }
                if (!isGet && HttpFields.__ContentLength.equals(hdr) && Integer.parseInt(val) > 0) {
                    hasContent = true;
                }

                connection.addRequestProperty(hdr, val);
                xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr);
            }
        }
    }

    // add any custom request headers that the user asked for
    Map<String, String> customRequestHeaders = AddCustomRequestHeaderCommand.getHeaders();
    for (Map.Entry<String, String> e : customRequestHeaders.entrySet()) {
        connection.addRequestProperty(e.getKey(), e.getValue());
        entry.addRequestHeader(e.getKey(), e.getValue());
    }

    // Proxy headers
    if (!_anonymous)
        connection.setRequestProperty("Via", "1.1 (jetty)");
    if (!xForwardedFor)
        connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr());

    // a little bit of cache control
    String cache_control = request.getField(HttpFields.__CacheControl);
    if (cache_control != null
            && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0))
        connection.setUseCaches(false);

    // customize Connection
    customizeConnection(pathInContext, pathParams, request, connection);

    try {
        connection.setDoInput(true);

        // do input thang!
        InputStream in = request.getInputStream();
        if (hasContent) {
            connection.setDoOutput(true);
            IO.copy(in, connection.getOutputStream());
        }

        // Connect
        connection.connect();
    } catch (Exception e) {
        LogSupport.ignore(log, e);
    }

    InputStream proxy_in = null;

    // handler status codes etc.
    int code = -1;
    if (http != null) {
        proxy_in = http.getErrorStream();

        try {
            code = http.getResponseCode();
        } catch (SSLHandshakeException e) {
            throw new RuntimeException("Couldn't establish SSL handshake.  Try using trustAllSSLCertificates.\n"
                    + e.getLocalizedMessage(), e);
        }
        response.setStatus(code);
        response.setReason(http.getResponseMessage());

        String contentType = http.getContentType();
        if (log.isDebugEnabled()) {
            log.debug("Content-Type is: " + contentType);
        }
    }

    if (proxy_in == null) {
        try {
            proxy_in = connection.getInputStream();
        } catch (Exception e) {
            LogSupport.ignore(log, e);
            proxy_in = http.getErrorStream();
        }
    }

    // clear response defaults.
    response.removeField(HttpFields.__Date);
    response.removeField(HttpFields.__Server);

    // set response headers
    int h = 0;
    String hdr = connection.getHeaderFieldKey(h);
    String val = connection.getHeaderField(h);
    while (hdr != null || val != null) {
        if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr)
                && (_chained || !_ProxyAuthHeaders.containsKey(hdr)))
            response.addField(hdr, val);
        h++;
        hdr = connection.getHeaderFieldKey(h);
        val = connection.getHeaderField(h);
    }
    if (!_anonymous)
        response.setField("Via", "1.1 (jetty)");

    response.removeField(HttpFields.__ETag); // possible cksum?  Stop caching...
    response.removeField(HttpFields.__LastModified); // Stop caching...

    // Handled
    long bytesCopied = -1;
    request.setHandled(true);
    if (proxy_in != null) {
        boolean injectableResponse = http.getResponseCode() == HttpURLConnection.HTTP_OK
                || (http.getResponseCode() >= 400 && http.getResponseCode() < 600);
        if (proxyInjectionMode && injectableResponse) {
            // check if we should proxy this path based on the dontProxyRegex that can be user-specified
            if (shouldInject(request.getPath())) {
                bytesCopied = InjectionHelper.injectJavaScript(request, response, proxy_in,
                        response.getOutputStream(), debugURL);
            } else {
                bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream());
            }
        } else {
            bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream());
        }
    }

    entry.finish(code, bytesCopied);
    entry.addResponseHeader(response);

    CaptureNetworkTrafficCommand.capture(entry);

    return bytesCopied;
}

From source file:cgeo.geocaching.cgBase.java

public static void postTweet(cgeoapplication app, cgSettings settings, String status, final Geopoint coords) {
    if (app == null) {
        return;//from   w  ww  .j  a v a  2 s  . co  m
    }
    if (settings == null || StringUtils.isBlank(settings.tokenPublic)
            || StringUtils.isBlank(settings.tokenSecret)) {
        return;
    }

    try {
        Map<String, String> parameters = new HashMap<String, String>();

        parameters.put("status", status);
        if (coords != null) {
            parameters.put("lat", String.format("%.6f", coords.getLatitude()));
            parameters.put("long", String.format("%.6f", coords.getLongitude()));
            parameters.put("display_coordinates", "true");
        }

        final String paramsDone = cgOAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false,
                parameters, settings.tokenPublic, settings.tokenSecret);

        HttpURLConnection connection = null;
        try {
            final StringBuffer buffer = new StringBuffer();
            final URL u = new URL("http://api.twitter.com/1/statuses/update.json");
            final URLConnection uc = u.openConnection();

            uc.setRequestProperty("Host", "api.twitter.com");

            connection = (HttpURLConnection) uc;
            connection.setReadTimeout(30000);
            connection.setRequestMethod("POST");
            HttpURLConnection.setFollowRedirects(true);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            final OutputStream out = connection.getOutputStream();
            final OutputStreamWriter wr = new OutputStreamWriter(out);
            wr.write(paramsDone);
            wr.flush();
            wr.close();

            Log.i(cgSettings.tag,
                    "Twitter.com: " + connection.getResponseCode() + " " + connection.getResponseMessage());

            InputStream ins;
            final String encoding = connection.getContentEncoding();

            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                ins = new GZIPInputStream(connection.getInputStream());
            } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
                ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
            } else {
                ins = connection.getInputStream();
            }

            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr);

            readIntoBuffer(br, buffer);

            br.close();
            ins.close();
            inr.close();
            connection.disconnect();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgBase.postTweet.IO: " + connection.getResponseCode() + ": "
                    + connection.getResponseMessage() + " ~ " + e.toString());

            final InputStream ins = connection.getErrorStream();
            final StringBuffer buffer = new StringBuffer();
            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr);

            readIntoBuffer(br, buffer);

            br.close();
            ins.close();
            inr.close();
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgBase.postTweet.inner: " + e.toString());
        }

        connection.disconnect();
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgBase.postTweet: " + e.toString());
    }
}

From source file:cgeo.geocaching.cgBase.java

public static String requestJSON(String scheme, String host, String path, String method, String params) {
    int httpCode = -1;
    //String httpLocation = null;

    if (method == null) {
        method = "GET";
    } else {// w w w.j  a  va 2s. co  m
        method = method.toUpperCase();
    }

    boolean methodPost = false;
    if (method.equalsIgnoreCase("POST")) {
        methodPost = true;
    }

    URLConnection uc = null;
    HttpURLConnection connection = null;
    Integer timeout = 30000;
    final StringBuffer buffer = new StringBuffer();

    for (int i = 0; i < 3; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }

        buffer.delete(0, buffer.length());
        timeout = 30000 + (i * 15000);

        try {
            try {
                URL u = null;
                if (methodPost) {
                    u = new URL(scheme + host + path);
                } else {
                    u = new URL(scheme + host + path + "?" + params);
                }

                if (u.getProtocol().toLowerCase().equals("https")) {
                    trustAllHosts();
                    HttpsURLConnection https = (HttpsURLConnection) u.openConnection();
                    https.setHostnameVerifier(doNotVerify);
                    uc = https;
                } else {
                    uc = (HttpURLConnection) u.openConnection();
                }

                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
                if (methodPost) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    uc.setRequestProperty("Content-Length", Integer.toString(params.length()));
                    uc.setRequestProperty("X-HTTP-Method-Override", "GET");
                } else {
                    uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                }
                uc.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                connection = (HttpURLConnection) uc;
                connection.setReadTimeout(timeout);
                connection.setRequestMethod(method);
                HttpURLConnection.setFollowRedirects(false); // TODO: Fix these (FilCab)
                connection.setDoInput(true);
                if (methodPost) {
                    connection.setDoOutput(true);

                    final OutputStream out = connection.getOutputStream();
                    final OutputStreamWriter wr = new OutputStreamWriter(out);
                    wr.write(params);
                    wr.flush();
                    wr.close();
                } else {
                    connection.setDoOutput(false);
                }

                InputStream ins = getInputstreamFromConnection(connection);
                final InputStreamReader inr = new InputStreamReader(ins);
                final BufferedReader br = new BufferedReader(inr, 1024);

                readIntoBuffer(br, buffer);

                httpCode = connection.getResponseCode();

                final String paramsLog = params.replaceAll(passMatch, "password=***");
                Log.i(cgSettings.tag + " | JSON",
                        "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | "
                                + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path
                                + "?" + paramsLog);

                connection.disconnect();
                br.close();
                ins.close();
                inr.close();
            } catch (IOException e) {
                httpCode = connection.getResponseCode();

                Log.e(cgSettings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": "
                        + connection.getResponseMessage() + " ~ " + e.toString());
            }
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSON: " + e.toString());
        }

        if (StringUtils.isNotBlank(buffer)) {
            break;
        }

        if (httpCode == 403) {
            // we're not allowed to download content, so let's move
            break;
        }
    }

    String page = null;
    //This is reported as beeing deadCode (httpLocation is always null)
    //2011-08-09 - 302 is redirect so something should probably be done
    /*
     * if (httpCode == 302 && httpLocation != null) {
     * final Uri newLocation = Uri.parse(httpLocation);
     * if (newLocation.isRelative()) {
     * page = requestJSONgc(host, path, params);
     * } else {
     * page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params);
     * }
     * } else {
     */
    replaceWhitespace(buffer);
    page = buffer.toString();
    //}

    if (page != null) {
        return page;
    } else {
        return "";
    }
}

From source file:cgeo.geocaching.cgBase.java

public String requestJSONgc(String host, String path, String params) {
    int httpCode = -1;
    String httpLocation = null;//from   ww  w  .jav a2s  .c om

    final String cookiesDone = CookieJar.getCookiesAsString(prefs);

    URLConnection uc = null;
    HttpURLConnection connection = null;
    Integer timeout = 30000;
    final StringBuffer buffer = new StringBuffer();

    for (int i = 0; i < 3; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }

        buffer.delete(0, buffer.length());
        timeout = 30000 + (i * 15000);

        try {
            // POST
            final URL u = new URL("http://" + host + path);
            uc = u.openConnection();

            uc.setRequestProperty("Host", host);
            uc.setRequestProperty("Cookie", cookiesDone);
            uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            uc.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
            uc.setRequestProperty("Referer", host + "/" + path);

            if (settings.asBrowser == 1) {
                uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
                uc.setRequestProperty("Accept-Language", "en-US");
                uc.setRequestProperty("User-Agent", idBrowser);
                uc.setRequestProperty("Connection", "keep-alive");
                uc.setRequestProperty("Keep-Alive", "300");
            }

            connection = (HttpURLConnection) uc;
            connection.setReadTimeout(timeout);
            connection.setRequestMethod("POST");
            HttpURLConnection.setFollowRedirects(false); // TODO: Fix these (FilCab)
            connection.setDoInput(true);
            connection.setDoOutput(true);

            final OutputStream out = connection.getOutputStream();
            final OutputStreamWriter wr = new OutputStreamWriter(out);
            wr.write(params);
            wr.flush();
            wr.close();

            CookieJar.setCookies(prefs, uc);

            InputStream ins = getInputstreamFromConnection(connection);
            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr);

            readIntoBuffer(br, buffer);

            httpCode = connection.getResponseCode();
            httpLocation = uc.getHeaderField("Location");

            final String paramsLog = params.replaceAll(passMatch, "password=***");
            Log.i(cgSettings.tag + " | JSON",
                    "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | "
                            + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?"
                            + paramsLog);

            connection.disconnect();
            br.close();
            ins.close();
            inr.close();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSONgc.IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.requestJSONgc: " + e.toString());
        }

        if (buffer != null && buffer.length() > 0) {
            break;
        }
    }

    String page = null;
    if (httpCode == 302 && httpLocation != null) {
        final Uri newLocation = Uri.parse(httpLocation);
        if (newLocation.isRelative()) {
            page = requestJSONgc(host, path, params);
        } else {
            page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params);
        }
    } else {
        replaceWhitespace(buffer);
        page = buffer.toString();
    }

    if (page != null) {
        return page;
    } else {
        return "";
    }
}

From source file:cgeo.geocaching.cgBase.java

public cgResponse request(boolean secure, String host, String path, String method, String params, int requestId,
        Boolean xContentType) {/*from   w  w  w.j a  v a2  s  .c  o m*/
    URL u = null;
    int httpCode = -1;
    String httpMessage = null;
    String httpLocation = null;

    if (requestId == 0) {
        requestId = (int) (Math.random() * 1000);
    }

    if (method == null
            || (method.equalsIgnoreCase("GET") == false && method.equalsIgnoreCase("POST") == false)) {
        method = "POST";
    } else {
        method = method.toUpperCase();
    }

    // https
    String scheme = "http://";
    if (secure) {
        scheme = "https://";
    }

    String cookiesDone = CookieJar.getCookiesAsString(prefs);

    URLConnection uc = null;
    HttpURLConnection connection = null;
    Integer timeout = 30000;
    StringBuffer buffer = null;

    for (int i = 0; i < 5; i++) {
        if (i > 0) {
            Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1));
        }

        buffer = new StringBuffer();
        timeout = 30000 + (i * 10000);

        try {
            if (method.equals("GET")) {
                // GET
                u = new URL(scheme + host + path + "?" + params);
                uc = u.openConnection();

                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Cookie", cookiesDone);
                if (xContentType) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                }

                if (settings.asBrowser == 1) {
                    uc.setRequestProperty("Accept",
                            "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network
                    uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
                    uc.setRequestProperty("Accept-Language", "en-US");
                    uc.setRequestProperty("User-Agent", idBrowser);
                    uc.setRequestProperty("Connection", "keep-alive");
                    uc.setRequestProperty("Keep-Alive", "300");
                }

                connection = (HttpURLConnection) uc;
                connection.setReadTimeout(timeout);
                connection.setRequestMethod(method);
                HttpURLConnection.setFollowRedirects(false);
                connection.setDoInput(true);
                connection.setDoOutput(false);
            } else {
                // POST
                u = new URL(scheme + host + path);
                uc = u.openConnection();

                uc.setRequestProperty("Host", host);
                uc.setRequestProperty("Cookie", cookiesDone);
                if (xContentType) {
                    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                }

                if (settings.asBrowser == 1) {
                    uc.setRequestProperty("Accept",
                            "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
                    // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network
                    uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
                    uc.setRequestProperty("Accept-Language", "en-US");
                    uc.setRequestProperty("User-Agent", idBrowser);
                    uc.setRequestProperty("Connection", "keep-alive");
                    uc.setRequestProperty("Keep-Alive", "300");
                }

                connection = (HttpURLConnection) uc;
                connection.setReadTimeout(timeout);
                connection.setRequestMethod(method);
                HttpURLConnection.setFollowRedirects(false);
                connection.setDoInput(true);
                connection.setDoOutput(true);

                final OutputStream out = connection.getOutputStream();
                final OutputStreamWriter wr = new OutputStreamWriter(out);
                wr.write(params);
                wr.flush();
                wr.close();
            }

            CookieJar.setCookies(prefs, uc);

            InputStream ins = getInputstreamFromConnection(connection);
            final InputStreamReader inr = new InputStreamReader(ins);
            final BufferedReader br = new BufferedReader(inr, 16 * 1024);

            readIntoBuffer(br, buffer);

            httpCode = connection.getResponseCode();
            httpMessage = connection.getResponseMessage();
            httpLocation = uc.getHeaderField("Location");

            final String paramsLog = params.replaceAll(passMatch, "password=***");
            Log.i(cgSettings.tag + "|" + requestId,
                    "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | "
                            + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?"
                            + paramsLog);

            connection.disconnect();
            br.close();
            ins.close();
            inr.close();
        } catch (IOException e) {
            Log.e(cgSettings.tag, "cgeoBase.request.IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(cgSettings.tag, "cgeoBase.request: " + e.toString());
        }

        if (buffer.length() > 0) {
            break;
        }
    }

    cgResponse response = new cgResponse();

    try {
        if (httpCode == 302 && httpLocation != null) {
            final Uri newLocation = Uri.parse(httpLocation);
            if (newLocation.isRelative()) {
                response = request(secure, host, path, "GET", new HashMap<String, String>(), requestId, false,
                        false, false);
            } else {
                boolean secureRedir = false;
                if (newLocation.getScheme().equals("https")) {
                    secureRedir = true;
                }
                response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET",
                        new HashMap<String, String>(), requestId, false, false, false);
            }
        } else {
            if (StringUtils.isNotEmpty(buffer)) {
                replaceWhitespace(buffer);
                String data = buffer.toString();
                buffer = null;

                if (data != null) {
                    response.setData(data);
                } else {
                    response.setData("");
                }
                response.setStatusCode(httpCode);
                response.setStatusMessage(httpMessage);
                response.setUrl(u.toString());
            }
        }
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgeoBase.page: " + e.toString());
    }

    return response;
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void setUserName(final String userName, final String password, final boolean syncChannels) {
    if (userName != null && password != null) {
        new Thread() {
            public void run() {
                URL documentUrl;/*w  w  w. ja va 2s .c  o  m*/
                try {
                    documentUrl = new URL("http://android.tvbrowser.org/data/scripts/testMyAccount.php");
                    URLConnection connection = documentUrl.openConnection();

                    String userpass = userName + ":" + password;
                    String basicAuth = "basic " + Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP);

                    connection.setRequestProperty("Authorization", basicAuth);

                    if (((HttpURLConnection) connection).getResponseCode() != 200) {
                        showUserError(userName, password, syncChannels);
                    } else {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                storeUserName(userName, password, syncChannels);
                            }
                        });
                    }

                } catch (Throwable t) {
                    showUserError(userName, password, syncChannels);
                }
            }
        }.start();
    } else if (syncChannels) {
        syncronizeChannels();
    }
}