List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:tkwatch.Utilities.java
/** * Issues a TradeKing API request. Adapted from the <i>TradeKing API * Reference Guide</i>, 03.25.2011, p. 51. * /*from w w w. ja va 2 s . c o m*/ * @param resourceUrl * The URL to which API requests must be made. * @param body * The body of the API request. * @param appKey * The user's application key. * @param userKey * The user's key. * @param userSecret * The user's secret key. * @return Returns the result of the API request. */ public static final String tradeKingRequest(final String resourceUrl, final String body, final String appKey, final String userKey, final String userSecret) { String response = new String(); try { String timestamp = String.valueOf(Calendar.getInstance().getTimeInMillis()); String request_data = body + timestamp; String signature = generateSignature(request_data, userSecret); URL url = new URL(resourceUrl); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/xml"); conn.setRequestProperty("Accept", "application/xml"); conn.setRequestProperty("TKI_TIMESTAMP", timestamp); conn.setRequestProperty("TKI_SIGNATURE", signature); conn.setRequestProperty("TKI_USERKEY", userKey); conn.setRequestProperty("TKI_APPKEY", appKey); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(body); out.flush(); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String temp; while ((temp = in.readLine()) != null) { response += temp + "\n"; } in.close(); return response; } catch (java.security.SignatureException e) { errorMessage(e.getMessage()); return ""; } catch (java.io.IOException e) { errorMessage(e.getMessage()); return ""; } }
From source file:tufts.vue.ds.XMLIngest.java
private static InputStream getTestXMLStream() throws IOException { // // SMF 2008-10-02: E.g. Craigslist XML streams use ISO-8859-1, which is provided in // // HTML headers as "Content-Type: application/rss+xml; charset=ISO-8859-1", (tho not // // in a special content-encoding header), and our current XML parser fails unless // // the stream is read with this set: e.g.: [org.xml.sax.SAXParseException: Character // // conversion error: "Unconvertible UTF-8 character beginning with 0x95" (line // // number may be too low).] Actually, in this case it turns out that providing a // // default InputStreamReader (encoding not specified) as opposed to a direct // // InputStream from the URLConnection works, and the XML parser is presumably then // // finding and handling the "<?xml version="1.0" encoding="ISO-8859-1"?>" line at // // the top of the XML stream // final XmlSchema schema = new XmlSchema(conn.getURL(), itemKey); // InputStream is = null; // try { // is = conn.getInputStream(); // errout("GOT INPUT STREAM: " + Util.tags(is)); // } catch (IOException e) { // e.printStackTrace(); // return null; // }/*ww w .j a v a 2 s . c o m*/ // final Document doc = parseXML(is, false); // Could also use a ROME API XmlReader(URLConnection) for handling // the input, which does it's own magic to figure out the encoding. // For more on the complexity of this issue, see: // http://diveintomark.org/archives/2004/02/13/xml-media-types URL url = new URL(JIRA_VUE_URL); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", JIRA_SFRAIZE_COOKIE); errout("Opening connection to " + url); conn.connect(); errout("Getting InputStream..."); InputStream in = conn.getInputStream(); errout("Got " + Util.tags(in)); errout("Getting headers..."); Map<String, List<String>> headers = conn.getHeaderFields(); errout("HEADERS:"); for (Map.Entry<String, List<String>> e : headers.entrySet()) { errout(e.getKey() + ": " + e.getValue()); } return in; }
From source file:com.frostwire.gui.library.LibraryUtils.java
private static InternetRadioStation processInternetRadioStationUrl(String urlStr) throws Exception { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setConnectTimeout(10000);/*www . jav a 2 s. c om*/ conn.setRequestProperty("User-Agent", "Java"); InputStream is = conn.getInputStream(); BufferedReader d = null; if (conn.getContentEncoding() != null) { d = new BufferedReader(new InputStreamReader(is, conn.getContentEncoding())); } else { d = new BufferedReader(new InputStreamReader(is)); } String pls = ""; String[] props = null; String strLine; int numLine = 0; while ((strLine = d.readLine()) != null) { pls += strLine + "\n"; if (strLine.startsWith("File1=")) { String streamUrl = strLine.split("=")[1]; props = processStreamUrl(streamUrl); } else if (strLine.startsWith("icy-name:")) { pls = ""; props = processStreamUrl(urlStr); break; } numLine++; if (numLine > 10) { if (props == null) { // not a valid pls break; } } } is.close(); if (props != null && props[0] != null) { return LibraryMediator.getLibrary().newInternetRadioStation(props[0], props[0], props[1], props[2], props[3], props[4], props[5], pls, false); } else { return null; } }
From source file:com.frostwire.gui.library.LibraryUtils.java
private static String[] processStreamUrl(String streamUrl) throws Exception { URL url = new URL(streamUrl); System.out.print(" - " + streamUrl); URLConnection conn = url.openConnection(); conn.setConnectTimeout(10000);/*from www.j a v a 2 s.com*/ conn.setRequestProperty("User-Agent", "Java"); InputStream is = conn.getInputStream(); BufferedReader d = null; if (conn.getContentEncoding() != null) { d = new BufferedReader(new InputStreamReader(is, conn.getContentEncoding())); } else { d = new BufferedReader(new InputStreamReader(is)); } String name = null; String genre = null; String website = null; String type = null; String br = null; String strLine; int i = 0; while ((strLine = d.readLine()) != null && i < 10) { if (strLine.startsWith("icy-name:")) { name = clean(strLine.split(":")[1]); } else if (strLine.startsWith("icy-genre:")) { genre = clean(strLine.split(":")[1]); } else if (strLine.startsWith("icy-url:")) { website = strLine.split("icy-url:")[1].trim(); } else if (strLine.startsWith("content-type:")) { String contentType = strLine.split(":")[1].trim(); if (contentType.equals("audio/aacp")) { type = "AAC+"; } else if (contentType.equals("audio/mpeg")) { type = "MP3"; } else if (contentType.equals("audio/aac")) { type = "AAC"; } } else if (strLine.startsWith("icy-br:")) { br = strLine.split(":")[1].trim() + " kbps"; } i++; } is.close(); return new String[] { name, streamUrl, br, type, website, genre }; }
From source file:org.nuxeo.rss.reader.FeedHelper.java
public static SyndFeed parseFeed(String feedUrl) throws Exception { URL url = new URL(feedUrl); SyndFeedInput input = new SyndFeedInput(); URLConnection urlConnection; if (FeedUrlConfig.useProxy()) { SocketAddress addr = new InetSocketAddress(FeedUrlConfig.getProxyHost(), FeedUrlConfig.getProxyPort()); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); urlConnection = url.openConnection(proxy); if (FeedUrlConfig.isProxyAuthenticated()) { String encoded = Base64.encodeBytes( new String(FeedUrlConfig.getProxyLogin() + ":" + FeedUrlConfig.getProxyPassword()) .getBytes()); urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded); urlConnection.connect();/*w w w .j av a 2s.c o m*/ } } else { urlConnection = url.openConnection(); } SyndFeed feed = input.build(new XmlReader(urlConnection)); return feed; }
From source file:com.roadwarrior.vtiger.client.NetworkUtilities.java
/** * Connects to the server, authenticates the provided username and * password./*from ww w . ja v a 2 s . co m*/ * * @param username The user's username * @param password The user's password * @return String The authentication token returned by the server (or null) */ public static String authenticate(String username, String accessKey, String base_url) { String token = null; String hash = null; authenticate_log_text = "authenticate()\n"; AUTH_URI = base_url + "/webservice.php"; authenticate_log_text = authenticate_log_text + "url: " + AUTH_URI + "?operation=getchallenge&username=" + username + "\n"; Log.d(TAG, "AUTH_URI : "); Log.d(TAG, AUTH_URI + "?operation=getchallenge&username=" + username); // =========== get challenge token ============================== ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); try { URL url; // HTTP GET REQUEST url = new URL(AUTH_URI + "?operation=getchallenge&username=" + username); HttpURLConnection con; con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("Content-length", "0"); con.setUseCaches(false); // for some site that redirects based on user agent con.setInstanceFollowRedirects(true); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.15) Gecko/2009101600 Firefox/3.0.15"); con.setAllowUserInteraction(false); int timeout = 20000; con.setConnectTimeout(timeout); con.setReadTimeout(timeout); con.connect(); int status = con.getResponseCode(); authenticate_log_text = authenticate_log_text + "Request status = " + status + "\n"; switch (status) { case 200: case 201: case 302: BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); Log.d(TAG, "message body"); Log.d(TAG, sb.toString()); authenticate_log_text = authenticate_log_text + "body : " + sb.toString(); if (status == 302) { authenticate_log_text = sb.toString(); return null; } JSONObject result = new JSONObject(sb.toString()); Log.d(TAG, result.getString("result")); JSONObject data = new JSONObject(result.getString("result")); token = data.getString("token"); break; case 401: Log.e(TAG, "Server auth error: ");// + readResponse(con.getErrorStream())); authenticate_log_text = authenticate_log_text + "Server auth error"; return null; default: Log.e(TAG, "connection status code " + status);// + readResponse(con.getErrorStream())); authenticate_log_text = authenticate_log_text + "connection status code :" + status; return null; } } catch (ClientProtocolException e) { Log.i(TAG, "getchallenge:http protocol error"); Log.e(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "ClientProtocolException :" + e.getMessage() + "\n"; return null; } catch (IOException e) { Log.e(TAG, "getchallenge: IO Exception"); Log.e(TAG, e.getMessage()); Log.e(TAG, AUTH_URI + "?operation=getchallenge&username=" + username); authenticate_log_text = authenticate_log_text + "getchallenge: IO Exception : " + e.getMessage() + "\n"; return null; } catch (JSONException e) { Log.i(TAG, "json exception"); authenticate_log_text = authenticate_log_text + "JSon exception\n"; // TODO Auto-generated catch block e.printStackTrace(); return null; } // ================= login ================== try { MessageDigest m = MessageDigest.getInstance("MD5"); m.update(token.getBytes()); m.update(accessKey.getBytes()); hash = new BigInteger(1, m.digest()).toString(16); Log.i(TAG, "hash"); Log.i(TAG, hash); } catch (NoSuchAlgorithmException e) { authenticate_log_text = authenticate_log_text + "MD5 => no such algorithm\n"; e.printStackTrace(); } try { String charset; charset = "utf-8"; String query = String.format("operation=login&username=%s&accessKey=%s", URLEncoder.encode(username, charset), URLEncoder.encode(hash, charset)); authenticate_log_text = authenticate_log_text + "login()\n"; URLConnection connection = new URL(AUTH_URI).openConnection(); connection.setDoOutput(true); // Triggers POST. int timeout = 20000; connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setAllowUserInteraction(false); connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); connection.setUseCaches(false); OutputStream output = connection.getOutputStream(); try { output.write(query.getBytes(charset)); } finally { try { output.close(); } catch (IOException logOrIgnore) { } } Log.d(TAG, "Query written"); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); Log.d(TAG, "message post body"); Log.d(TAG, sb.toString()); authenticate_log_text = authenticate_log_text + "post message body :" + sb.toString() + "\n"; JSONObject result = new JSONObject(sb.toString()); String success = result.getString("success"); Log.i(TAG, success); if (success == "true") { Log.i(TAG, result.getString("result")); Log.i(TAG, "sucesssfully logged in is"); JSONObject data = new JSONObject(result.getString("result")); sessionName = data.getString("sessionName"); Log.i(TAG, sessionName); authenticate_log_text = authenticate_log_text + "successfully logged in\n"; return token; } else { // success is false, retrieve error JSONObject data = new JSONObject(result.getString("error")); authenticate_log_text = "can not login :\n" + data.toString(); return null; } //token = data.getString("token"); //Log.i(TAG,token); } catch (ClientProtocolException e) { Log.d(TAG, "login: http protocol error"); Log.d(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "HTTP Protocol error \n"; authenticate_log_text = authenticate_log_text + e.getMessage() + "\n"; } catch (IOException e) { Log.d(TAG, "login: IO Exception"); Log.d(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "login: IO Exception \n"; authenticate_log_text = authenticate_log_text + e.getMessage() + "\n"; } catch (JSONException e) { Log.d(TAG, "JSON exception"); // TODO Auto-generated catch block authenticate_log_text = authenticate_log_text + "JSON exception "; authenticate_log_text = authenticate_log_text + e.getMessage(); e.printStackTrace(); } return null; // ======================================================================== }
From source file:com.norconex.commons.lang.url.URLStreamer.java
/** * Streams URL content.//from w w w . j a v a 2s . com * @param url the URL to stream * @param creds credentials for a protected URL * @param proxy proxy to use to stream the URL * @param proxyCreds credentials to access the proxy * @return a URL content InputStream */ public static InputStream stream(String url, Credentials creds, HttpHost proxy, Credentials proxyCreds) { try { URLConnection conn = null; if (proxy != null) { if (LOG.isDebugEnabled()) { LOG.debug("Streaming with proxy: " + proxy.getHostName() + ":" + proxy.getPort()); } Proxy p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHostName(), proxy.getPort())); //Authenticator. conn = new URL(url).openConnection(p); if (proxyCreds != null) { if (LOG.isDebugEnabled()) { LOG.debug("Streaming with proxy credentials."); } conn.setRequestProperty("Proxy-Authorization", base64BasicAuth(proxyCreds.getUsername(), proxyCreds.getPassword())); } } else { conn = new URL(url).openConnection(); } if (creds != null) { if (LOG.isDebugEnabled()) { LOG.debug("Streaming with credentials."); } conn.setRequestProperty("Authorization", base64BasicAuth(creds.getUsername(), creds.getPassword())); } return responseInputStream(conn); } catch (IOException e) { throw new URLException("Could not stream URL: " + url, e); } }
From source file:edu.stanford.muse.slant.CustomSearchHelper.java
/** returns an oauth token for the user's CSE. returns null if login failed. */ public static String authenticate(String login, String password) { //System.out.println("About to post\nURL: "+target+ "content: " + content); String authToken = null;// w ww .j av a 2 s .c o m String response = ""; try { URL url = new URL("https://www.google.com/accounts/ClientLogin"); URLConnection conn = url.openConnection(); // Set connection parameters. conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); // Make server believe we are form data... conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); // TODO: escape password, we'll fail if password has & // login = "musetestlogin1"; // password = "whowonthelottery"; String content = "accountType=HOSTED_OR_GOOGLE&Email=" + login + "&Passwd=" + password + "&service=cprose&source=muse.chrome.extension"; out.writeBytes(content); out.flush(); out.close(); // Read response from the input stream. BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String temp; while ((temp = in.readLine()) != null) { response += temp + "\n"; } temp = null; in.close(); log.info("Obtaining auth token: Server response:\n'" + response + "'"); String delimiter = "Auth="; /* given string will be split by the argument delimiter provided. */ String[] token = response.split(delimiter); authToken = token[1]; authToken = authToken.trim(); authToken = authToken.replaceAll("(\\r|\\n)", ""); log.info("auth token: " + authToken); return authToken; } catch (Exception e) { log.warn("Unable to authorize: " + e.getMessage()); return null; } }
From source file:org.norvelle.addressdiscoverer.gui.threading.ExtractIndividualsFromUrlWorker.java
/** * Run the classification process on the contents of a remote web page. * /*w w w .j a va 2 s . co m*/ * @param parent * @param uri * @param department * @throws java.net.MalformedURLException * @throws java.net.URISyntaxException */ public ExtractIndividualsFromUrlWorker(EmailDiscoveryPanel parent, String uri, Department department, boolean useSequentialParser) throws MalformedURLException, URISyntaxException, IOException { super(parent, null, department, useSequentialParser); StructuredPageContactLinkLocator.baseUrl = uri; parent.getjStageNameLabel().setText("Reading remote web page"); URL u = new URL(uri); // this would check for the protocol u.toURI(); URLConnection con = u.openConnection(); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; " + "en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); File tempDir = FileUtils.getTempDirectory(); File tempFile = new File(tempDir.getAbsolutePath() + File.separator + "classifier.html.tmp"); FileUtils.write(tempFile, body, encoding); this.fileToClassify = tempFile; parent.getjBytesReceivedLabel().setText(String.valueOf(body.length())); }
From source file:android.provider.CredentialInputAdapter.java
@Override public URLConnection getConnection() throws IOException { if (getURL().getProtocol().equals("ftp")) { URL u = new URL("ftp://" + credentials.getUsername() + ":" + credentials.getPassword() + "@" + getURL().toExternalForm().substring(6)); return u.openConnection(); } else if (getURL().getProtocol().equals("http")) { Base64 enc = new Base64(); byte[] encoded = enc.encode((credentials.getUsername() + ":" + credentials.getPassword()).getBytes()); URLConnection connection = getURL().openConnection(); connection.setRequestProperty("Authorization", "Basic " + new String(encoded)); return connection; }/* w w w. j a v a 2 s . c om*/ return super.getConnection(); }