public static void setRequestOptions(HttpRequestBase request, HttpRequestOptions requestOptions) { request.getParams().setParameter(AllClientPNames.MAX_REDIRECTS, new Integer(requestOptions.getMaxRedirects())); request.getParams().setParameter(AllClientPNames.SO_TIMEOUT, new Integer(requestOptions.getSocketTimeout())); request.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, new Integer(requestOptions.getConnTimeout())); request.getParams().setParameter(AllClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.valueOf(requestOptions.getAllowCircularRedirects())); Map requestHeaders = requestOptions.getRequestHeaders(); if (requestHeaders != null) { Iterator iter = requestHeaders.keySet().iterator(); String headerName; while (iter.hasNext()) { headerName = (String) iter.next(); request.addHeader(headerName, (String) requestHeaders.get(headerName)); } } }
@Override public WebResourceResponse loadUrl(String url) { NetflixUrl netflixUrl = new NetflixUrl(url); // note due to shouldOverrideUrlLoading, no /watch url ever gets here, so no need to cater for it if (netflixUrl.isNetflixUrl()) { client.getParams().setParameter(AllClientPNames.USER_AGENT, UserAgents.Mobile); if (netflixUrl.isTitle()) { client.getParams().setParameter(AllClientPNames.USER_AGENT, UserAgents.Desktop); return doLoadUrl(url); } else if (netflixUrl.isBrowse() || netflixUrl.isDefault()) { return doLoadUrl(url); } } // else return null; }
/** * Send get to URL. * * @param url * @return */ public static String sendGet(String url) { HttpClient httpClient = new DefaultHttpClient(); String content = null; try { if(url.indexOf("https") != -1){ httpClient = wrapClient(httpClient); } HttpGet httpGet = new HttpGet(url); // 请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 40000); // 读取超时 httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 40000); httpClient.getParams().setParameter(AllClientPNames.STRICT_TRANSFER_ENCODING,"GBK"); content = httpClient.execute(httpGet, new BasicResponseHandler()); } catch (Exception e) { log.error("Get url faild, url: " + url, e); content = null; } finally { httpClient.getConnectionManager().shutdown(); } return content; }
private HttpClient getHttpClient() { if (this.httpClient == null) { LOG.log(Level.FINER, "Creating an HttpClient"); BasicHttpParams params = new BasicHttpParams(); params.setParameter(AllClientPNames.CONNECTION_TIMEOUT, this.connectionTimeout) .setParameter(AllClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH) .setParameter(AllClientPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8) .setParameter(AllClientPNames.SO_TIMEOUT, this.socketTimeout); this.httpClient = new DefaultHttpClient(params); } return this.httpClient; }
public static String sendGet(String url, String encoding, String ip) { HttpClient httpClient = new DefaultHttpClient(); String content = null; try { if(url.indexOf("https") != -1){ httpClient = wrapClient(httpClient); } HttpGet httpGet = new HttpGet(url); if(ip != null){ httpGet.setHeader("X-Forwarded-For", ip); } // 请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 40000); // 读取超时 httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 40000); httpClient.getParams().setParameter(AllClientPNames.STRICT_TRANSFER_ENCODING, encoding); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1 content = EntityUtils.toString(entity, encoding); } } catch (Exception e) { e.printStackTrace(); log.error("Get url faild, url: " + url, e); content = null; } finally { httpClient.getConnectionManager().shutdown(); } return content; }
/** * Send a payload message. * * @param payload * @throws IOException */ public void send(Payload payload) throws IOException { String room = payload.getRoom(); String token; if (StringUtils.isEmpty(room)) { // default room room = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_ROOM, null); token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN, null); } else { // specified room, validate token token = runtimeManager.getSettings().getString(String.format(Plugin.SETTING_ROOM_TOKEN, room), null); if (StringUtils.isEmpty(token)) { room = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_ROOM, null); token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN, null); log.warn("No HipChat API token specified for '{}', defaulting to '{}'", payload.getRoom(), room); log.warn("Please set '{} = TOKEN' in gitblit.properties", String.format(Plugin.SETTING_ROOM_TOKEN, room)); } } String hipchatUrl = String.format("https://api.hipchat.com/v2/room/%s/notification?auth_token=%s", room, token); Gson gson = new GsonBuilder().create(); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(hipchatUrl); post.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Constants.NAME + "/" + Constants.getVersion()); post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 5000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 5000); String body = gson.toJson(payload); StringEntity entity = new StringEntity(body, "UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = client.execute(post); int rc = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_NO_CONTENT == rc) { // This is the expected result code // https://www.hipchat.com/docs/apiv2/method/send_room_notification // replace this with post.closeConnection() after JGit updates to HttpClient 4.2 post.abort(); } else { String result = null; InputStream is = response.getEntity().getContent(); try { byte [] buffer = new byte[8192]; ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0; while ((len = is.read(buffer)) > -1) { os.write(buffer, 0, len); } result = os.toString("UTF-8"); } finally { if (is != null) { is.close(); } } log.error("HipChat plugin sent:"); log.error(body); log.error("HipChat returned:"); log.error(result); throw new IOException(String.format("HipChat Error (%s): %s", rc, result)); } }
/** * Send a payload message. * * @param payload * @throws IOException */ public void send(Payload payload) throws IOException { String conversation = payload.getConversation(); String token; if (StringUtils.isEmpty(conversation)) { // default conversation token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN, null); } else { // specified conversation, validate token token = runtimeManager.getSettings().getString(String.format(Plugin.SETTING_CONVERSATION_TOKEN, conversation), null); if (StringUtils.isEmpty(token)) { token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN, null); log.warn("No Glip API token specified for '{}', defaulting to default conversation'", payload.getConversation()); log.warn("Please set '{} = TOKEN' in gitblit.properties", String.format(Plugin.SETTING_CONVERSATION_TOKEN, conversation)); } } Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GmtDateTypeAdapter()).create(); String json = gson.toJson(payload); log.debug(json); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT, 5000); client.getParams().setParameter(AllClientPNames.SO_TIMEOUT, 5000); String conversationUrl = payload.getEndPoint(token); HttpPost post = new HttpPost(conversationUrl); post.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Constants.NAME + "/" + Constants.getVersion()); post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); // post as JSON StringEntity entity = new StringEntity(json, "UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = client.execute(post); int rc = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == rc) { // This is the expected result code // replace this with post.closeConnection() after JGit updates to HttpClient 4.2 post.abort(); } else { String result = null; InputStream is = response.getEntity().getContent(); try { byte [] buffer = new byte[8192]; ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0; while ((len = is.read(buffer)) > -1) { os.write(buffer, 0, len); } result = os.toString("UTF-8"); } finally { if (is != null) { is.close(); } } log.error("Glip plugin sent:"); log.error(json); log.error("Glip returned:"); log.error(result); throw new IOException(String.format("Glip Error (%s): %s", rc, result)); } }
public void setUserAgent(String ua) { httpParams.setParameter(AllClientPNames.USER_AGENT, ua); }
public void setConnectionTimeout(int milliseconds) { httpParams.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, milliseconds); }
public void setSocketTimeout(int milliseconds) { httpParams.setIntParameter(AllClientPNames.SO_TIMEOUT, milliseconds); }