List of usage examples for org.apache.http.impl.client DefaultHttpClient setCookieStore
public synchronized void setCookieStore(final CookieStore cookieStore)
From source file:com.num.mobiperf.Checkin.java
/** * Return an appropriately-configured HTTP client. *//*from www . j ava 2 s .c o m*/ private HttpClient getNewHttpClient() { DefaultHttpClient client; try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new MySSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpConnectionParams.setConnectionTimeout(params, POST_TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(params, POST_TIMEOUT_MILLISEC); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); client = new DefaultHttpClient(ccm, params); } catch (Exception e) { // Logger.w("Unable to create SSL HTTP client", e); client = new DefaultHttpClient(); } // TODO(mdw): For some reason this is not sending the cookie to the // test server, probably because the cookie itself is not properly // initialized. Below I manually set the Cookie header instead. CookieStore store = new BasicCookieStore(); store.addCookie(authCookie); client.setCookieStore(store); return client; }
From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java
public void requery(String tableName, Class clazz, String key) throws ClientProtocolException, IOException, JSONException, Exception { Log.i(TAG, "Got key " + key + " but couldn't find related parent tr, requerying ..."); String url = FlowzrSyncOptions.FLOWZR_API_URL + options.getNamespace() + "/key/?tableName=" + DatabaseHelper.TRANSACTION_TABLE + "&key=" + key; StringBuilder builder = new StringBuilder(); DefaultHttpClient http_client2 = new DefaultHttpClient(); http_client2.setCookieStore(http_client.getCookieStore()); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = http_client2.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); InputStream content = httpEntity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line;/*from w ww. j a v a 2 s.c o m*/ while ((line = reader.readLine()) != null) { builder.append(line); } JSONObject o = new JSONObject(builder.toString()).getJSONArray(DatabaseHelper.TRANSACTION_TABLE) .getJSONObject(0); saveEntityFromJson(o, tableName, clazz, 1); }
From source file:sand.actionhandler.weibo.UdaClient.java
public static AccessToken getToken(String userid, String password) throws Exception { //https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=http://116.236.101.196:18080/weibo.WeiBoAH.weibo&response_type=code&state=&scope= String url = "https://api.weibo.com/oauth2/authorize"; String content = ""; DefaultHttpClient httpclient = new DefaultHttpClient(); try {/* w w w .j a v a 2 s .c o m*/ httpclient = createHttpClient(); HttpPost httppost = new HttpPost(url); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("client_id", "2517196057")); nameValuePairs.add(new BasicNameValuePair("redirect_uri", REDIRECT_URI)); nameValuePairs.add(new BasicNameValuePair("userId", userid)); nameValuePairs.add(new BasicNameValuePair("passwd", password)); nameValuePairs.add(new BasicNameValuePair("isLoginSina", "")); nameValuePairs.add(new BasicNameValuePair("action", "submit")); nameValuePairs.add(new BasicNameValuePair("response_type", "code")); nameValuePairs.add(new BasicNameValuePair("withOfficalFlag", "0")); nameValuePairs.add(new BasicNameValuePair("ticket", "")); nameValuePairs.add(new BasicNameValuePair("'regCallback'", "")); nameValuePairs.add(new BasicNameValuePair("state", "")); nameValuePairs.add(new BasicNameValuePair("from", "")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // headers.add(new Header("Referer", "https://api.weibo.com/oauth2/authorize?client_id=your_client_id&redirect_uri=your_redirect_url&from=sina&response_type=code"));//referer // headers.add(new Header("Host", "api.weibo.com")); // headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0")); httppost.addHeader("Referer", "https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=" + REDIRECT_URI + "&from=sina&response_type=code"); httppost.addHeader("Host", "api.weibo.com"); httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"); httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2965); httppost.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); CookieStore cookieStore = new BasicCookieStore(); addCookie(cookieStore); // httpclient.setCookieStore(cookieStore); //httpclient.ex System.out.println(httppost.getURI()); System.out.println("https://api.weibo.com/oauth2/authorize?client_id=2517196057&redirect_uri=" + REDIRECT_URI + "&from=sina&response_type=code"); HttpResponse response = httpclient.execute(httppost); System.out.println("response " + response.getAllHeaders()); for (Header h : response.getAllHeaders()) { System.out.println(h.getName() + ", " + h.getValue()); } logger.info("response " + response); Header location = response.getFirstHeader("Location"); logger.info("location " + location); System.out.println("location " + location); if (location != null) { String retUrl = location.getValue(); System.out.println(retUrl); logger.info("retUrl " + retUrl); //String reU = location.getValue(); int begin = retUrl.indexOf("code="); if (begin != -1) { int end = retUrl.indexOf("&", begin); if (end == -1) end = retUrl.length(); String code = retUrl.substring(begin + 5, end); if (code != null) { Oauth oauth = new Oauth(); System.out.println("code is " + code); AccessToken token = oauth.getAccessTokenByCode(code); return token; } } } // HttpEntity entity = response.getEntity(); // // if (entity != null) { // content = EntityUtils.toString(entity); // System.out.println(content); // System.out.println("----------------------------------------"); // System.out.println("Uncompressed size: "+content.length()); // } } catch (Exception e) { // TODO Auto-generated catch block logger.error("error", e); e.printStackTrace(); throw e; } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } return null; }
From source file:com.mockey.ClientExecuteProxy.java
/** * /*from w ww. j ava 2 s. com*/ * @param twistInfo * @param proxyServer * @param realServiceUrl * @param httpMethod * @param request * @return * @throws ClientExecuteProxyException */ public ResponseFromService execute(TwistInfo twistInfo, ProxyServerModel proxyServer, Url realServiceUrl, boolean allowRedirectFollow, RequestFromClient request) throws ClientExecuteProxyException { log.info("Request: " + String.valueOf(realServiceUrl)); // general setup SchemeRegistry supportedSchemes = new SchemeRegistry(); // Register the "http" and "https" protocol schemes, they are // required by the default operator to look up socket factories. supportedSchemes.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); supportedSchemes.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); // prepare parameters HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.ISO_8859_1); HttpProtocolParams.setUseExpectContinue(params, false); ClientConnectionManager ccm = new ThreadSafeClientConnManager(supportedSchemes); DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params); if (!allowRedirectFollow) { // Do NOT allow for 302 REDIRECT httpclient.setRedirectStrategy(new DefaultRedirectStrategy() { public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) { boolean isRedirect = false; try { isRedirect = super.isRedirected(request, response, context); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!isRedirect) { int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 301 || responseCode == 302) { return true; } } return isRedirect; } }); } else { // Yes, allow for 302 REDIRECT // Nothing needed here. } // Prevent CACHE, 304 not modified // httpclient.addRequestInterceptor(new HttpRequestInterceptor() { // public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { // // request.setHeader("If-modified-Since", "Fri, 13 May 2006 23:54:18 GMT"); // // } // }); CookieStore cookieStore = httpclient.getCookieStore(); for (Cookie httpClientCookie : request.getHttpClientCookies()) { // HACK: // httpClientCookie.getValue(); cookieStore.addCookie(httpClientCookie); } // httpclient.setCookieStore(cookieStore); if (ClientExecuteProxy.cookieStore == null) { ClientExecuteProxy.cookieStore = httpclient.getCookieStore(); } else { httpclient.setCookieStore(ClientExecuteProxy.cookieStore); } StringBuffer requestCookieInfo = new StringBuffer(); // Show what cookies are in the store . for (Cookie cookie : ClientExecuteProxy.cookieStore.getCookies()) { log.debug("Cookie in the cookie STORE: " + cookie.toString()); requestCookieInfo.append(cookie.toString() + "\n\n\n"); } if (proxyServer.isProxyEnabled()) { // make sure to use a proxy that supports CONNECT httpclient.getCredentialsProvider().setCredentials(proxyServer.getAuthScope(), proxyServer.getCredentials()); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyServer.getHttpHost()); } // TWISTING Url originalRequestUrlBeforeTwisting = null; if (twistInfo != null) { String fullurl = realServiceUrl.getFullUrl(); String twistedUrl = twistInfo.getTwistedValue(fullurl); if (twistedUrl != null) { originalRequestUrlBeforeTwisting = realServiceUrl; realServiceUrl = new Url(twistedUrl); } } ResponseFromService responseMessage = null; try { HttpHost htttphost = new HttpHost(realServiceUrl.getHost(), realServiceUrl.getPort(), realServiceUrl.getScheme()); HttpResponse response = httpclient.execute(htttphost, request.postToRealServer(realServiceUrl)); if (response.getStatusLine().getStatusCode() == 302) { log.debug("FYI: 302 redirect occuring from " + realServiceUrl.getFullUrl()); } responseMessage = new ResponseFromService(response); responseMessage.setOriginalRequestUrlBeforeTwisting(originalRequestUrlBeforeTwisting); responseMessage.setRequestUrl(realServiceUrl); } catch (Exception e) { log.error(e); throw new ClientExecuteProxyException("Unable to retrieve a response. ", realServiceUrl, e); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } // Parse out the response information we're looking for // StringBuffer responseCookieInfo = new StringBuffer(); // // Show what cookies are in the store . // for (Cookie cookie : ClientExecuteProxy.cookieStore.getCookies()) { // log.info("Cookie in the cookie STORE: " + cookie.toString()); // responseCookieInfo.append(cookie.toString() + "\n\n\n"); // // } // responseMessage.setRequestCookies(requestCookieInfo.toString()); // responseMessage.setResponseCookies(responseCookieInfo.toString()); return responseMessage; }
From source file:fr.eolya.utils.http.HttpLoader.java
/** * @param /*from w w w . j av a2s . c om*/ * @return */ private HttpClient getHttpClient(String url) { try { // ClientConnectionManager SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); registry.register(new Scheme("https", 443, sf)); ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); // Params HttpParams httpParams = getHttpParams(); // DefaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(ccm, httpParams); // Proxy setProxy(httpClient, url, proxyHost, proxyPort, proxyExclude, proxyUserName, proxyPassword); // if (StringUtils.isNotEmpty(proxyHost)) { // if (StringUtils.isNotEmpty(proxyUserName) && StringUtils.isNotEmpty(proxyPassword)) { // httpClient.getCredentialsProvider().setCredentials( // new AuthScope(proxyHost,Integer.valueOf(proxyPort)), // new UsernamePasswordCredentials(proxyUserName, proxyPassword)); // } // HttpHost proxy = new HttpHost(proxyHost,Integer.valueOf(proxyPort)); // httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); // } else { // httpClient.getParams().removeParameter(ConnRoutePNames.DEFAULT_PROXY); // } // Cookies if (cookies != null) { CookieStore cookieStore = new BasicCookieStore(); Iterator<Entry<String, String>> it = cookies.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next(); BasicClientCookie cookie = new BasicClientCookie(pairs.getKey(), pairs.getValue()); //cookie.setDomain("your domain"); cookie.setPath("/"); cookieStore.addCookie(cookie); } httpClient.setCookieStore(cookieStore); } return new DecompressingHttpClient(httpClient); } catch (Exception e) { return new DecompressingHttpClient(new DefaultHttpClient()); } }
From source file:com.google.acre.script.AcreFetch.java
@SuppressWarnings("boxing") public void fetch(boolean system, String response_encoding, boolean log_to_user, boolean no_redirect) { if (request_url.length() > 2047) { throw new AcreURLFetchException("fetching URL failed - url is too long"); }/*www . jav a 2 s.co m*/ DefaultHttpClient client = new DefaultHttpClient(_connectionManager, null); HttpParams params = client.getParams(); // pass the deadline down to the invoked service. // this will be ignored unless we are fetching from another // acre server. // note that we may send a deadline that is already passed: // it's not our job to throw here since we don't know how // the target service will interpret the quota header. // NOTE: this is done *after* the user sets the headers to overwrite // whatever settings they might have tried to change for this value // (which could be a security hazard) long sub_deadline = (HostEnv.LIMIT_EXECUTION_TIME) ? _deadline - HostEnv.SUBREQUEST_DEADLINE_ADVANCE : System.currentTimeMillis() + HostEnv.ACRE_URLFETCH_TIMEOUT; int reentries = _reentries + 1; request_headers.put(HostEnv.ACRE_QUOTAS_HEADER, "td=" + sub_deadline + ",r=" + reentries); // if this is not an internal call, we need to invoke the call thru a proxy if (!_internal) { // XXX No sense wasting the resources to gzip inside the network. // XXX seems that twitter gets upset when we do this /* if (!request_headers.containsKey("accept-encoding")) { request_headers.put("accept-encoding", "gzip"); } */ String proxy_host = Configuration.Values.HTTP_PROXY_HOST.getValue(); int proxy_port = -1; if (!(proxy_host.length() == 0)) { proxy_port = Configuration.Values.HTTP_PROXY_PORT.getInteger(); HttpHost proxy = new HttpHost(proxy_host, proxy_port, "http"); params.setParameter(AllClientPNames.DEFAULT_PROXY, proxy); } } params.setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // in msec long timeout = _deadline - System.currentTimeMillis(); if (timeout < 0) timeout = 0; params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, (int) timeout); params.setParameter(AllClientPNames.SO_TIMEOUT, (int) timeout); // we're not streaming the request so this should be a win. params.setParameter(AllClientPNames.TCP_NODELAY, true); // reuse an existing socket if it is in TIME_WAIT state. params.setParameter(AllClientPNames.SO_REUSEADDR, true); // set the encoding of our POST payloads to UTF-8 params.setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, "UTF-8"); BasicCookieStore cstore = new BasicCookieStore(); for (AcreCookie cookie : request_cookies.values()) { cstore.addCookie(cookie.toClientCookie()); } client.setCookieStore(cstore); HttpRequestBase method; HashMap<String, String> logmsg = new HashMap<String, String>(); logmsg.put("Method", request_method); logmsg.put("URL", request_url); params.setParameter(AllClientPNames.HANDLE_REDIRECTS, !no_redirect); logmsg.put("Redirect", Boolean.toString(!no_redirect)); try { if (request_method.equals("GET")) { method = new HttpGet(request_url); } else if (request_method.equals("POST")) { method = new HttpPost(request_url); } else if (request_method.equals("HEAD")) { method = new HttpHead(request_url); } else if (request_method.equals("PUT")) { method = new HttpPut(request_url); } else if (request_method.equals("DELETE")) { method = new HttpDelete(request_url); } else if (request_method.equals("PROPFIND")) { method = new HttpPropFind(request_url); } else { throw new AcreURLFetchException("Failed: unsupported (so far) method " + request_method); } method.getParams().setBooleanParameter(AllClientPNames.USE_EXPECT_CONTINUE, false); } catch (java.lang.IllegalArgumentException e) { throw new AcreURLFetchException("Unable to fetch URL; this is most likely an issue with URL encoding."); } catch (java.lang.IllegalStateException e) { throw new AcreURLFetchException("Unable to fetch URL; possibly an illegal protocol?"); } StringBuilder request_header_log = new StringBuilder(); for (Map.Entry<String, String> header : request_headers.entrySet()) { String key = header.getKey(); String value = header.getValue(); // XXX should suppress cookie headers? // content-type and length? if ("content-type".equalsIgnoreCase(key)) { Matcher m = contentTypeCharsetPattern.matcher(value); if (m.find()) { content_type = m.group(1); content_type_charset = m.group(2); } else { content_type_charset = "utf-8"; } method.addHeader(key, value); } else if ("content-length".equalsIgnoreCase(key)) { // ignore user-supplied content-length, which is // probably wrong due to chars vs bytes and is // redundant anyway ArrayList<String> msg = new ArrayList<String>(); msg.add("User-supplied content-length header is ignored"); _acre_response.log("warn", msg); } else if ("user-agent".equalsIgnoreCase(key)) { params.setParameter(AllClientPNames.USER_AGENT, value); } else { method.addHeader(key, value); } if (!("x-acre-auth".equalsIgnoreCase(key))) { request_header_log.append(key + ": " + value + "\r\n"); } } logmsg.put("Headers", request_header_log.toString()); // XXX need more detailed error checking if (method instanceof HttpEntityEnclosingRequestBase && request_body != null) { HttpEntityEnclosingRequestBase em = (HttpEntityEnclosingRequestBase) method; try { if (request_body instanceof String) { StringEntity ent = new StringEntity((String) request_body, content_type_charset); em.setEntity(ent); } else if (request_body instanceof JSBinary) { ByteArrayEntity ent = new ByteArrayEntity(((JSBinary) request_body).get_data()); em.setEntity(ent); } } catch (UnsupportedEncodingException e) { throw new AcreURLFetchException( "Failed to fetch URL. " + " - Unsupported charset: " + content_type_charset); } } if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch request"); msg.add(logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.request", logmsg); long startTime = System.currentTimeMillis(); try { // this sends the http request and waits HttpResponse hres = client.execute(method); status = hres.getStatusLine().getStatusCode(); HashMap<String, String> res_logmsg = new HashMap<String, String>(); res_logmsg.put("URL", request_url); res_logmsg.put("Status", ((Integer) status).toString()); Header content_type_header = null; // translate response headers StringBuilder response_header_log = new StringBuilder(); Header[] rawheaders = hres.getAllHeaders(); for (Header rawheader : rawheaders) { String headername = rawheader.getName().toLowerCase(); if (headername.equalsIgnoreCase("content-type")) { content_type_header = rawheader; // XXX should strip everything after ; content_type = rawheader.getValue(); // XXX don't set content_type_parameters, deprecated? } else if (headername.equalsIgnoreCase("x-metaweb-cost")) { _costCollector.merge(rawheader.getValue()); } else if (headername.equalsIgnoreCase("x-metaweb-tid")) { res_logmsg.put("ITID", rawheader.getValue()); } headers.put(headername, rawheader.getValue()); response_header_log.append(headername + ": " + rawheader.getValue() + "\r\n"); } res_logmsg.put("Headers", response_header_log.toString()); if (!system && log_to_user) { ArrayList<Object> msg = new ArrayList<Object>(); msg.add("urlfetch response"); msg.add(res_logmsg); _acre_response.log("debug", msg); } _logger.info("urlfetch.response", res_logmsg); // read cookies for (Cookie c : cstore.getCookies()) { cookies.put(c.getName(), new AcreCookie(c)); } // get body encoding String charset = null; if (content_type_header != null) { HeaderElement values[] = content_type_header.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } if (charset == null) charset = response_encoding; // read body HttpEntity ent = hres.getEntity(); if (ent != null) { InputStream res_stream = ent.getContent(); Header cenc = ent.getContentEncoding(); if (cenc != null && res_stream != null) { HeaderElement[] codecs = cenc.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase("gzip")) { res_stream = new GZIPInputStream(res_stream); } } } long firstByteTime = 0; long endTime = 0; if (content_type != null && (content_type.startsWith("image/") || content_type.startsWith("application/octet-stream") || content_type.startsWith("multipart/form-data"))) { // HttpClient's InputStream doesn't support mark/reset, so // wrap it with one that does. BufferedInputStream bufis = new BufferedInputStream(res_stream); bufis.mark(2); bufis.read(); firstByteTime = System.currentTimeMillis(); bufis.reset(); byte[] data = IOUtils.toByteArray(bufis); endTime = System.currentTimeMillis(); body = new JSBinary(); ((JSBinary) body).set_data(data); try { if (res_stream != null) { res_stream.close(); } } catch (IOException e) { // ignore } } else if (res_stream == null || charset == null) { firstByteTime = endTime = System.currentTimeMillis(); body = ""; } else { StringWriter writer = new StringWriter(); Reader reader = new InputStreamReader(res_stream, charset); int i = reader.read(); firstByteTime = System.currentTimeMillis(); writer.write(i); IOUtils.copy(reader, writer); endTime = System.currentTimeMillis(); body = writer.toString(); try { reader.close(); writer.close(); } catch (IOException e) { // ignore } } long waitingTime = firstByteTime - startTime; long readingTime = endTime - firstByteTime; _logger.debug("urlfetch.timings", "waiting time: " + waitingTime + "ms"); _logger.debug("urlfetch.timings", "reading time: " + readingTime + "ms"); Statistics.instance().collectUrlfetchTime(startTime, firstByteTime, endTime); _costCollector.collect((system) ? "asuc" : "auuc").collect((system) ? "asuw" : "auuw", waitingTime) .collect((system) ? "asub" : "auub", waitingTime); } } catch (IllegalArgumentException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("failed to fetch URL. " + " - Request Error: " + cause.getMessage()); } catch (IOException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } catch (RuntimeException e) { Throwable cause = e.getCause(); if (cause == null) cause = e; throw new AcreURLFetchException("Failed to fetch URL. " + " - Network Error: " + cause.getMessage()); } finally { method.abort(); } }
From source file:com.zoffcc.applications.aagtl.HTMLDownloader.java
public boolean login() { // System.out.println("--L--- LOGIN START -----"); String login_url = "https://www.geocaching.com/login/default.aspx"; DefaultHttpClient client2 = null; HttpHost proxy = null;//from w w w. java2 s . c o m if (CacheDownloader.DEBUG2_) { // NEVER enable this on a production release!!!!!!!!!! // NEVER enable this on a production release!!!!!!!!!! trust_Every_ssl_cert(); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443)); HttpParams params = new BasicHttpParams(); params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30)); params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); proxy = new HttpHost("192.168.0.1", 8888); params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry); client2 = new DefaultHttpClient(cm, params); // NEVER enable this on a production release!!!!!!!!!! // NEVER enable this on a production release!!!!!!!!!! } else { HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 10000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 10000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); client2 = new DefaultHttpClient(httpParameters); } if (CacheDownloader.DEBUG2_) { client2.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } // read first page to check for login // read first page to check for login // read first page to check for login String websiteData2 = null; URI uri2 = null; try { uri2 = new URI(login_url); } catch (URISyntaxException e) { e.printStackTrace(); return false; } client2.setCookieStore(cookie_jar); HttpGet method2 = new HttpGet(uri2); method2.addHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10"); method2.addHeader("Pragma", "no-cache"); method2.addHeader("Accept-Language", "en"); HttpResponse res = null; try { res = client2.execute(method2); } catch (ClientProtocolException e1) { e1.printStackTrace(); return false; } catch (IOException e1) { e1.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } InputStream data = null; try { data = res.getEntity().getContent(); } catch (IllegalStateException e1) { e1.printStackTrace(); return false; } catch (IOException e1) { e1.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } websiteData2 = generateString(data); if (CacheDownloader.DEBUG_) System.out.println("111 cookies=" + dumpCookieStore(client2.getCookieStore())); cookie_jar = client2.getCookieStore(); // on every page final Matcher matcherLogged2In = patternLogged2In.matcher(websiteData2); if (matcherLogged2In.find()) { if (CacheDownloader.DEBUG_) System.out.println("login: -> already logged in (1)"); return true; } // after login final Matcher matcherLoggedIn = patternLoggedIn.matcher(websiteData2); if (matcherLoggedIn.find()) { if (CacheDownloader.DEBUG_) System.out.println("login: -> already logged in (2)"); return true; } // read first page to check for login // read first page to check for login // read first page to check for login // ok post login data as formdata // ok post login data as formdata // ok post login data as formdata // ok post login data as formdata HttpPost method = new HttpPost(uri2); method.addHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0) Firefox/7.0"); method.addHeader("Pragma", "no-cache"); method.addHeader("Content-Type", "application/x-www-form-urlencoded"); method.addHeader("Accept-Language", "en"); List<NameValuePair> loginInfo = new ArrayList<NameValuePair>(); loginInfo.add(new BasicNameValuePair("__EVENTTARGET", "")); loginInfo.add(new BasicNameValuePair("__EVENTARGUMENT", "")); String[] viewstates = getViewstates(websiteData2); if (CacheDownloader.DEBUG_) System.out.println("vs==" + viewstates[0]); putViewstates(loginInfo, viewstates); loginInfo.add(new BasicNameValuePair("ctl00$ContentBody$tbUsername", this.main_aagtl.global_settings.options_username)); loginInfo.add(new BasicNameValuePair("ctl00$ContentBody$tbPassword", this.main_aagtl.global_settings.options_password)); loginInfo.add(new BasicNameValuePair("ctl00$ContentBody$btnSignIn", "Login")); loginInfo.add(new BasicNameValuePair("ctl00$ContentBody$cbRememberMe", "on")); //for (int i = 0; i < loginInfo.size(); i++) //{ // System.out.println("x*X " + loginInfo.get(i).getName() + " " + loginInfo.get(i).getValue()); //} // set cookies client2.setCookieStore(cookie_jar); HttpEntity entity = null; try { entity = new UrlEncodedFormEntity(loginInfo, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return false; } // try // { // System.out.println("1 " + entity.toString()); // InputStream in2 = entity.getContent(); // InputStreamReader ir2 = new InputStreamReader(in2, "utf-8"); // BufferedReader r = new BufferedReader(ir2); // System.out.println("line=" + r.readLine()); // } // catch (Exception e2) // { // e2.printStackTrace(); // } method.setEntity(entity); HttpResponse res2 = null; try { res2 = client2.execute(method); if (CacheDownloader.DEBUG_) System.out.println("login response ->" + String.valueOf(res2.getStatusLine())); } catch (ClientProtocolException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } // for (int x = 0; x < res2.getAllHeaders().length; x++) // { // System.out.println("## n=" + res2.getAllHeaders()[x].getName()); // System.out.println("## v=" + res2.getAllHeaders()[x].getValue()); // } InputStream data2 = null; try { data2 = res2.getEntity().getContent(); } catch (IllegalStateException e1) { e1.printStackTrace(); return false; } catch (IOException e1) { e1.printStackTrace(); return false; } catch (Exception e) { e.printStackTrace(); return false; } websiteData2 = generateString(data2); if (CacheDownloader.DEBUG_) System.out.println("2222 cookies=" + dumpCookieStore(client2.getCookieStore())); cookie_jar = client2.getCookieStore(); String[] viewstates2 = getViewstates(websiteData2); if (CacheDownloader.DEBUG_) System.out.println("vs==" + viewstates2[0]); // System.out.println("******"); // System.out.println("******"); // System.out.println("******"); // System.out.println(websiteData2); // System.out.println("******"); // System.out.println("******"); // System.out.println("******"); return true; }
From source file:org.debux.webmotion.test.ActionMiscIT.java
@Test public void cookieManagerRead() throws IOException, URISyntaxException { URI uri = createRequest("/cookie/read").addParameter("secured", "false").build(); HttpGet request = new HttpGet(uri); DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); client.setCookieStore(cookieStore); BasicClientCookie initialCookie = new BasicClientCookie("name", "test"); initialCookie.setVersion(1);/*from w w w .ja v a 2s . c o m*/ initialCookie.setDomain("localhost"); initialCookie.setPath("/webmotion-test/test/cookie"); cookieStore.addCookie(initialCookie); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); String result = IOUtils.toString(content); AssertJUnit.assertTrue(result, result.contains("Value = test")); }
From source file:org.debux.webmotion.test.ActionMiscIT.java
@Test public void cookieManagerObjectRemove() throws IOException, URISyntaxException { URI uri = createRequest("/cookie/object/remove").build(); HttpGet request = new HttpGet(uri); DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); client.setCookieStore(cookieStore); BasicClientCookie initialCookie = new BasicClientCookie("user_cookie", "{}"); initialCookie.setVersion(1);/*from w w w. java 2 s .c om*/ initialCookie.setDomain("localhost"); initialCookie.setPath("/webmotion-test/test/cookie/object"); cookieStore.addCookie(initialCookie); client.execute(request); List<Cookie> cookies = cookieStore.getCookies(); for (Cookie cookie : cookies) { String name = cookie.getName(); if ("user_cookie".equals(name)) { throw new RuntimeException("Invalid cookie"); } } }
From source file:org.debux.webmotion.test.ActionMiscIT.java
@Test public void cookieManagerObjectRead() throws IOException, URISyntaxException { URI uri = createRequest("/cookie/object/read").build(); HttpGet request = new HttpGet(uri); DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); client.setCookieStore(cookieStore); BasicClientCookie initialCookie = new BasicClientCookie("user_cookie", "{\\\"value\\\":\\\"test\\\"}"); initialCookie.setVersion(1);/*from w w w. j a v a 2s. c o m*/ initialCookie.setDomain("localhost"); initialCookie.setPath("/webmotion-test/test/cookie/object"); cookieStore.addCookie(initialCookie); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); String result = IOUtils.toString(content); AssertJUnit.assertTrue(result, result.contains("Current value = test")); }