List of usage examples for org.apache.http.params HttpParams setParameter
HttpParams setParameter(String str, Object obj);
From source file:bkampfbot.plan.PlanArbeiten.java
public final static boolean finish() throws FatalError { try {//w w w. j a v a2 s.c o m // HTTP parameters stores header etc. HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); HttpGet httpget = new HttpGet(Config.getHost() + "arbeitsamt/index"); httpget.setParams(params); HttpResponse response = Control.current.httpclient.execute(httpget); // obtain redirect target Header locationHeader = response.getFirstHeader("location"); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { resEntity.consumeContent(); } if (locationHeader != null) { if (locationHeader.getValue() .equalsIgnoreCase((Config.getHost() + "arbeitsamt/serve").toLowerCase())) { try { JSONTokener js = new JSONTokener(Utils.getString("services/serviceData")); JSONObject result = new JSONObject(js); // [truncated] // {"currentTime":43,"fullTime":3600,"urlCancel":"\/arbeitsamt\/cancel","urlFinish":"\/arbeitsamt\/finish","workTotalTime":"1","workFee":"112","workFeeType":"gold","workText":"Die // Kellnerin im Goldenen Igel kommt nach einem langen int seconds = result.getInt("fullTime") - result.getInt("currentTime") + 20; Output.printTabLn("Letzte Arbeit wurde nicht beendet. Schlafe " + Math.round(seconds / 60) + " Minuten.", 1); // cut it into parts to safe session while (seconds > 600) { // sleep for 10 min Control.sleep(6000, 2); seconds -= 600; Control.current.getCharacter(); } Control.sleep(10 * seconds, 2); Utils.visit("arbeitsamt/finish"); } catch (JSONException e) { Output.error(e); return false; } } else if (locationHeader.getValue() .equalsIgnoreCase((Config.getHost() + "arbeitsamt/finish").toLowerCase())) { Utils.visit("arbeitsamt/finish"); } else { Output.println("Es ging was schief.", 0); return false; } } } catch (IOException e) { Output.println("Es ging was schief.", 0); return false; } return true; }
From source file:dk.moerks.ratebeermobile.io.NetBroker.java
private static DefaultHttpClient init() { HttpParams params = new BasicHttpParams(); params.setParameter("CookiePolicy", CookiePolicy.BROWSER_COMPATIBILITY); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", new PlainSocketFactory(), 80)); registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); return new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params); }
From source file:org.xwiki.android.authenticator.rest.XWikiConnector.java
public static HttpGet createHtpGet(String url, String user, String pass) { HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ':' + pass).getBytes(), Base64.NO_WRAP)); HttpParams params = new BasicHttpParams(); params.setParameter("username", user); params.setParameter("password", pass); httpGet.setParams(params);/*from www . ja v a2 s. c o m*/ httpGet.addHeader("Accept", "application/xml"); return httpGet; }
From source file:org.xwiki.android.authenticator.rest.XWikiConnector.java
public static String userSignIn(String server, String user, String pass, String authType) throws Exception { Log.d("xwiki", "userSignIn"); DefaultHttpClient httpClient = new DefaultHttpClient(); String url = server + "/rest/"; HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ':' + pass).getBytes(), Base64.NO_WRAP)); HttpParams params = new BasicHttpParams(); params.setParameter("username", user); params.setParameter("password", pass); httpGet.setParams(params);/*from w ww . ja v a 2s. c o m*/ HttpResponse response = httpClient.execute(httpGet); int error = response.getStatusLine().getStatusCode(); if (error != 200) { throw new Exception("Error signing-in [" + url + "] with error code [" + error + "]"); } return null; }
From source file:org.apache.jmeter.protocol.http.sampler.HttpClientDefaultParameters.java
/** * Loads a property file and converts parameters as necessary. * // w w w . j a v a2s. c om * @param file the file to load * @param params Apache HttpClient parameter instance */ public static void load(String file, final org.apache.http.params.HttpParams params) { load(file, new GenericHttpParams() { @Override public void setParameter(String name, Object value) { params.setParameter(name, value); } @Override public void setVersion(String name, String value) { String[] parts = value.split("\\."); if (parts.length != 2) { throw new IllegalArgumentException("Version must have form m.n"); } params.setParameter(name, new org.apache.http.HttpVersion(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]))); } }); }
From source file:org.lol.reddit.account.RedditAccount.java
public static LoginResultPair login(final Context context, final String username, final String password, final HttpClient client) { final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(3); fields.add(new BasicNameValuePair("user", username)); fields.add(new BasicNameValuePair("passwd", password)); fields.add(new BasicNameValuePair("api_type", "json")); // TODO put somewhere else final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO remove hardcoded params, put in network prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 5); final HttpPost request = new HttpPost("https://ssl.reddit.com/api/login"); request.setParams(params);/* www .j av a 2 s . com*/ try { request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { return new LoginResultPair(LoginResult.INTERNAL_ERROR); } final PersistentCookieStore cookies = new PersistentCookieStore(); final HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookies); final StatusLine status; final HttpEntity entity; try { final HttpResponse response = client.execute(request, localContext); status = response.getStatusLine(); entity = response.getEntity(); } catch (IOException e) { return new LoginResultPair(LoginResult.CONNECTION_ERROR); } if (status.getStatusCode() != 200) { return new LoginResultPair(LoginResult.REQUEST_ERROR); } final JsonValue result; try { result = new JsonValue(entity.getContent()); result.buildInThisThread(); } catch (IOException e) { return new LoginResultPair(LoginResult.CONNECTION_ERROR); } final String modhash; try { // TODO use the more general reddit error finder final JsonBufferedArray errors = result.asObject().getObject("json").getArray("errors"); errors.join(); if (errors.getCurrentItemCount() != 0) { for (final JsonValue v : errors) { for (final JsonValue s : v.asArray()) { // TODO handle unknown messages by concatenating all Reddit's strings if (s.getType() == JsonValue.Type.STRING) { Log.i("RR DEBUG ERROR", s.asString()); // lol, reddit api if (s.asString().equalsIgnoreCase("WRONG_PASSWORD") || s.asString().equalsIgnoreCase("invalid password") || s.asString().equalsIgnoreCase("passwd")) return new LoginResultPair(LoginResult.WRONG_PASSWORD); if (s.asString().contains("too much")) // also "RATELIMIT", but that's not as descriptive return new LoginResultPair(LoginResult.RATELIMIT, s.asString()); } } } return new LoginResultPair(LoginResult.UNKNOWN_REDDIT_ERROR); } final JsonBufferedObject data = result.asObject().getObject("json").getObject("data"); modhash = data.getString("modhash"); } catch (NullPointerException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } catch (InterruptedException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } catch (IOException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } return new LoginResultPair(LoginResult.SUCCESS, new RedditAccount(username, modhash, cookies, 0), null); }
From source file:org.cm.podd.report.util.RequestDataUtil.java
public static ResponseObject get(String path, String query, String token) { JSONObject jsonObj = null;/*from ww w.ja va 2 s. co m*/ String rawData = null; int statusCode = 0; //SharedPreferences settings = PoddApplication.getAppContext().getSharedPreferences("PoddPrefsFile", 0); String serverUrl = settings.getString("serverUrl", BuildConfig.SERVER_URL); String reqUrl = ""; if (path.contains("http://") || path.contains("https://")) { reqUrl = String.format("%s%s", path, query == null ? "" : "?" + query); } else { reqUrl = String.format("%s%s%s", serverUrl, path, query == null ? "" : "?" + query); } Log.i(TAG, "submit url=" + reqUrl); HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient client = new DefaultHttpClient(params); try { HttpGet get = new HttpGet(reqUrl); if (token != null) { get.setHeader("Authorization", "Token " + token); } HttpResponse response; response = client.execute(get); HttpEntity entity = response.getEntity(); // Detect server complaints statusCode = response.getStatusLine().getStatusCode(); Log.v(TAG, "status code=" + statusCode); if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_CREATED) { InputStream in = entity.getContent(); rawData = FileUtil.convertInputStreamToString(in); entity.consumeContent(); } } catch (ClientProtocolException e) { Log.e(TAG, "error post data", e); } catch (IOException e) { Log.e(TAG, "Can't connect server", e); } finally { client.getConnectionManager().shutdown(); } ResponseObject respObj = new ResponseObject(statusCode, jsonObj); respObj.setRawData(rawData); return respObj; }
From source file:org.cm.podd.report.util.RequestDataUtil.java
public static ResponseObject registerDeviceId(String deviceId, String token) { JSONObject jsonObj = null;//from w ww.jav a 2s.c o m int statusCode = 0; //SharedPreferences settings = PoddApplication.getAppContext().getSharedPreferences("PoddPrefsFile", 0); String serverUrl = settings.getString("serverUrl", BuildConfig.SERVER_URL); String reqUrl = serverUrl + "/gcm/"; Log.i(TAG, "submit url=" + reqUrl); HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient client = new DefaultHttpClient(params); try { HttpPost post = new HttpPost(reqUrl); post.setHeader("Content-Type", "application/json"); if (token != null) { post.setHeader("Authorization", "Token " + token); } JSONObject data = new JSONObject(); try { data.put("gcmRegId", deviceId); } catch (JSONException e) { Log.e(TAG, "Error while create json object", e); } post.setEntity(new StringEntity(data.toString(), HTTP.UTF_8)); HttpResponse response; response = client.execute(post); HttpEntity entity = response.getEntity(); // Detect server complaints statusCode = response.getStatusLine().getStatusCode(); Log.v(TAG, "status code=" + statusCode); if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_CREATED) { InputStream in = entity.getContent(); String resp = FileUtil.convertInputStreamToString(in); Log.d(TAG, "Register device id : Response text= " + resp); jsonObj = new JSONObject(); entity.consumeContent(); } } catch (ClientProtocolException e) { Log.e(TAG, "error post data", e); } catch (IOException e) { Log.e(TAG, "Can't connect server", e); } finally { client.getConnectionManager().shutdown(); } return new ResponseObject(statusCode, jsonObj); }
From source file:org.cm.podd.report.util.RequestDataUtil.java
public static ResponseObject post(String path, String query, String json, String token) { JSONObject jsonObj = null;/* w w w . ja v a 2 s .com*/ int statusCode = 0; //SharedPreferences settings = PoddApplication.getAppContext().getSharedPreferences("PoddPrefsFile", 0); String serverUrl = settings.getString("serverUrl", BuildConfig.SERVER_URL); String reqUrl = ""; if (path.contains("http://") || path.contains("https://")) { reqUrl = String.format("%s%s", path, query == null ? "" : "?" + query); } else { reqUrl = String.format("%s%s%s", serverUrl, path, query == null ? "" : "?" + query); } Log.i(TAG, "submit url=" + reqUrl); Log.i(TAG, "post data=" + json); HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient client = new DefaultHttpClient(params); try { HttpPost post = new HttpPost(reqUrl); post.setHeader("Content-Type", "application/json"); if (token != null) { post.setHeader("Authorization", "Token " + token); } post.setEntity(new StringEntity(json, HTTP.UTF_8)); HttpResponse response; response = client.execute(post); HttpEntity entity = response.getEntity(); // Detect server complaints statusCode = response.getStatusLine().getStatusCode(); Log.v(TAG, "status code=" + statusCode); if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_CREATED || statusCode == HttpURLConnection.HTTP_BAD_REQUEST) { InputStream in = entity.getContent(); String resp = FileUtil.convertInputStreamToString(in); jsonObj = new JSONObject(resp); entity.consumeContent(); } } catch (ClientProtocolException e) { Log.e(TAG, "error post data", e); } catch (IOException e) { Log.e(TAG, "Can't connect server", e); } catch (JSONException e) { Log.e(TAG, "error convert json", e); } finally { client.getConnectionManager().shutdown(); } return new ResponseObject(statusCode, jsonObj); }
From source file:com.ryan.ryanreader.account.RedditAccount.java
public static LoginResultPair login(final Context context, final String username, final String password, final HttpClient client) { // ???/*w w w .j a v a 2s . com*/ final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(3); fields.add(new BasicNameValuePair("user", username)); fields.add(new BasicNameValuePair("passwd", password)); fields.add(new BasicNameValuePair("api_type", "json")); // TODO put somewhere else final HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.USER_AGENT, Constants.ua(context)); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); // TODO // remove // hardcoded // params, // put // in // network // prefs params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); params.setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 100); params.setParameter(ClientPNames.HANDLE_REDIRECTS, true); params.setParameter(ClientPNames.MAX_REDIRECTS, 5); final HttpPost request = new HttpPost("https://ssl.reddit.com/api/login"); request.setParams(params); try { request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { return new LoginResultPair(LoginResult.INTERNAL_ERROR); } final PersistentCookieStore cookies = new PersistentCookieStore(); final HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookies); final StatusLine status; final HttpEntity entity; try { final HttpResponse response = client.execute(request, localContext); status = response.getStatusLine(); entity = response.getEntity(); } catch (IOException e) { return new LoginResultPair(LoginResult.CONNECTION_ERROR); } if (status.getStatusCode() != 200) { return new LoginResultPair(LoginResult.REQUEST_ERROR); } final JsonValue result; try { result = new JsonValue(entity.getContent()); result.buildInThisThread(); } catch (IOException e) { return new LoginResultPair(LoginResult.CONNECTION_ERROR); } final String modhash; try { // TODO use the more general reddit error finder final JsonBufferedArray errors = result.asObject().getObject("json").getArray("errors"); errors.join(); if (errors.getCurrentItemCount() != 0) { for (final JsonValue v : errors) { for (final JsonValue s : v.asArray()) { // TODO handle unknown messages by concatenating all // Reddit's strings if (s.getType() == JsonValue.Type.STRING) { Log.i("RR DEBUG ERROR", s.asString()); // lol, reddit api if (s.asString().equalsIgnoreCase("WRONG_PASSWORD") || s.asString().equalsIgnoreCase("invalid password") || s.asString().equalsIgnoreCase("passwd")) return new LoginResultPair(LoginResult.WRONG_PASSWORD); if (s.asString().contains("too much")) // also // "RATELIMIT", // but // that's // not as // descriptive return new LoginResultPair(LoginResult.RATELIMIT, s.asString()); } } } return new LoginResultPair(LoginResult.UNKNOWN_REDDIT_ERROR); } final JsonBufferedObject data = result.asObject().getObject("json").getObject("data"); modhash = data.getString("modhash"); } catch (NullPointerException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } catch (InterruptedException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } catch (IOException e) { return new LoginResultPair(LoginResult.JSON_ERROR); } return new LoginResultPair(LoginResult.SUCCESS, new RedditAccount(username, modhash, cookies, 0), null); }