List of usage examples for org.apache.http.protocol HTTP USER_AGENT
String USER_AGENT
To view the source code for org.apache.http.protocol HTTP USER_AGENT.
Click Source Link
From source file:org.yamj.api.common.http.HttpClientWrapper.java
@SuppressWarnings("unused") protected void prepareRequest(HttpHost target, HttpRequest request) throws ClientProtocolException { if (randomUserAgent) { request.setHeader(HTTP.USER_AGENT, UserAgentSelector.randomUserAgent()); }/* w w w . ja v a2s. com*/ }
From source file:groovesquid.Grooveshark.java
public static String sendRequest(String method, HashMap<String, Object> parameters) { if (tokenExpires <= new Date().getTime() && tokenExpires != 0 && !"initiateSession".equals(method) && !"getCommunicationToken".equals(method) && !"getCountry".equals(method)) { try {//w w w .java2 s . c om InitThread initThread = new InitThread(); initThread.start(); initThread.getLatch().await(); } catch (InterruptedException ex) { log.log(Level.SEVERE, null, ex); } } String responseContent = null; HttpEntity httpEntity = null; try { Client client = clients.getHtmlshark(); String protocol = "http://"; if (method.equals("getCommunicationToken")) { protocol = "https://"; } String url = protocol + "grooveshark.com/more.php?" + method; for (String jsqueueMethod : jsqueueMethods) { if (jsqueueMethod.equals(method)) { client = clients.getJsqueue(); break; } } header.put("client", client.getName()); header.put("clientRevision", client.getRevision()); header.put("privacy", "0"); header.put("uuid", uuid); header.put("country", country); if (!method.equals("initiateSession")) { header.put("session", session); header.put("token", generateToken(method, client.getSecret())); } Gson gson = new Gson(); String jsonString = gson.toJson(new JsonRequest(header, parameters, method)); log.info(">>> " + jsonString); HttpPost httpPost = new HttpPost(url); httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json"); httpPost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); httpPost.setHeader(HTTP.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31"); httpPost.setHeader("Referer", "http://grooveshark.com/JSQueue.swf?" + client.getRevision()); httpPost.setHeader("Content-Language", "en-US"); httpPost.setHeader("Cache-Control", "max-age=0"); httpPost.setHeader("Accept", "*/*"); httpPost.setHeader("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.3"); httpPost.setHeader("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4"); httpPost.setHeader("Accept-Encoding", "gzip,deflate,sdch"); httpPost.setHeader("Origin", "http://grooveshark.com"); if (!method.equals("initiateSession")) { httpPost.setHeader("Cookie", "PHPSESSID=" + session); } httpPost.setEntity(new StringEntity(jsonString, "UTF-8")); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (Main.getConfig().getProxyHost() != null && Main.getConfig().getProxyPort() != null) { httpClientBuilder .setProxy(new HttpHost(Main.getConfig().getProxyHost(), Main.getConfig().getProxyPort())); } HttpClient httpClient = httpClientBuilder.build(); HttpResponse httpResponse = httpClient.execute(httpPost); ByteArrayOutputStream baos = new ByteArrayOutputStream(); httpEntity = httpResponse.getEntity(); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == HttpStatus.SC_OK) { httpEntity.writeTo(baos); } else { throw new RuntimeException("method " + method + ": " + statusLine); } responseContent = baos.toString("UTF-8"); } catch (Exception ex) { Logger.getLogger(Grooveshark.class.getName()).log(Level.SEVERE, null, ex); } finally { try { EntityUtils.consume(httpEntity); } catch (IOException ex) { Logger.getLogger(Grooveshark.class.getName()).log(Level.SEVERE, null, ex); } } log.info("<<< " + responseContent); return responseContent; }
From source file:com.moviejukebox.tools.YamjHttpClient.java
@Override protected void prepareRequest(HttpHost target, HttpRequest request) throws ClientProtocolException { if (target.getHostName().contains("thetvdb")) { // a workaround for the need to use a referrer for thetvdb.com request.setHeader("Referer", "http://forums.thetvdb.com/"); if (ACCEPT_LANGUAGE != null) { request.setHeader("Accept-Language", ACCEPT_LANGUAGE); }//from w w w. jav a 2 s . com } else if (target.getHostName().contains("kinopoisk")) { // a workaround for the kinopoisk.ru site request.setHeader("Accept", "text/html, text/plain"); request.setHeader("Accept-Language", "ru"); request.setHeader(HTTP.USER_AGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6"); } else if (ACCEPT_LANGUAGE != null) { request.setHeader("Accept-Language", ACCEPT_LANGUAGE); } super.prepareRequest(target, request); }
From source file:com.handsome.frame.android.volley.stack.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError { HashMap<String, String> map = new HashMap<String, String>(); if (!TextUtils.isEmpty(mUserAgent)) { map.put(HTTP.USER_AGENT, mUserAgent); }/*from w w w .j a v a 2 s. co m*/ map.putAll(request.getHeaders()); URL parsedUrl = new URL(request.getUrl()); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } if (HandsomeApplication.getCookies() != null) { HDLog.d(TAG + "-Req-Cookie:", HandsomeApplication.getCookies().toString()); connection.addRequestProperty("Cookie", HandsomeApplication.getCookies()); } else { HDLog.d(TAG + "-Req-Cookie:", "null"); } setConnectionParametersForRequest(connection, request); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { if (header.getKey().equalsIgnoreCase("Set-Cookie")) { List<String> cookies = header.getValue(); HashMap<String, HttpCookie> cookieMap = new HashMap<String, HttpCookie>(); for (String string : cookies) { List<HttpCookie> cookie = HttpCookie.parse(string); for (HttpCookie httpCookie : cookie) { cookieMap.put(httpCookie.getName(), httpCookie); } } HandsomeApplication.setCookies(cookieMap); HDLog.d(TAG + "-Rsp-Cookie:", HandsomeApplication.getCookies().toString()); } else { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } } return response; }
From source file:com.omertron.omdbapi.OmdbApi.java
private String requestWebPage(URL url) throws OMDBException { LOG.trace("Requesting: {}", url.toString()); try {// w ww . j av a 2 s . co m final HttpGet httpGet = new HttpGet(url.toURI()); httpGet.addHeader("accept", "application/json"); httpGet.addHeader(HTTP.USER_AGENT, UserAgentSelector.randomUserAgent()); final DigestedResponse response = DigestedResponseReader.requestContent(httpClient, httpGet, charset); if (response.getStatusCode() >= HTTP_STATUS_500) { throw new OMDBException(ApiExceptionType.HTTP_503_ERROR, response.getContent(), response.getStatusCode(), url); } else if (response.getStatusCode() >= HTTP_STATUS_300) { throw new OMDBException(ApiExceptionType.HTTP_404_ERROR, response.getContent(), response.getStatusCode(), url); } return response.getContent(); } catch (URISyntaxException ex) { throw new OMDBException(ApiExceptionType.INVALID_URL, "Invalid URL", url, ex); } catch (IOException ex) { throw new OMDBException(ApiExceptionType.CONNECTION_ERROR, "Error retrieving URL", url, ex); } }
From source file:org.ops4j.pax.url.mvn.internal.wagon.ConfigurableHttpWagon.java
@Override protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException { setHeaders(httpMethod);// w w w.j a v a 2 s. co m String userAgent = getUserAgent(httpMethod); if (userAgent != null) { httpMethod.setHeader(HTTP.USER_AGENT, userAgent); } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); // WAGON-273: default the cookie-policy to browser compatible requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); Repository repo = getRepository(); ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost()); if (proxyInfo != null) { HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); requestConfigBuilder.setProxy(proxy); } HttpMethodConfiguration config = getHttpConfiguration() == null ? null : getHttpConfiguration().getMethodConfiguration(httpMethod); if (config != null) { copyConfig(config, requestConfigBuilder); } else { requestConfigBuilder.setSocketTimeout(getReadTimeout()); requestConfigBuilder.setConnectTimeout(getTimeout()); } getLocalContext().setRequestConfig(requestConfigBuilder.build()); if (config != null && config.isUsePreemptive()) { HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (getCredentialsProvider().getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); getAuthCache().put(targetHost, targetAuth); } } if (proxyInfo != null) { if (proxyInfo.getHost() != null) { HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost); String proxyUsername = proxyInfo.getUserName(); String proxyPassword = proxyInfo.getPassword(); String proxyNtlmHost = proxyInfo.getNtlmHost(); String proxyNtlmDomain = proxyInfo.getNtlmDomain(); if (proxyUsername != null && proxyPassword != null) { Credentials creds; if (proxyNtlmHost != null || proxyNtlmDomain != null) { creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain); } else { creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); } getCredentialsProvider().setCredentials(proxyScope, creds); BasicScheme proxyAuth = new BasicScheme(); proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive")); getAuthCache().put(proxyHost, proxyAuth); } } } return client.execute(httpMethod, getLocalContext()); }
From source file:ai.eve.volley.stack.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request) throws IOException, AuthFailureError { HashMap<String, String> map = new HashMap<String, String>(); if (!TextUtils.isEmpty(mUserAgent)) { map.put(HTTP.USER_AGENT, mUserAgent); }//from w ww . j a v a 2s. c o m if (!TextUtils.isEmpty(mSignInfo)) { map.put("SIGN", ESecurity.Encrypt(mSignInfo)); } map.putAll(request.getHeaders()); URL parsedUrl = new URL(request.getUrl()); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } if (EApplication.getCookies() != null) { ELog.I("cookie", EApplication.getCookies().toString()); connection.addRequestProperty("Cookie", EApplication.getReqCookies()); } else { ELog.I("cookie", "null"); } setConnectionParametersForRequest(connection, request); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { if (header.getKey().equalsIgnoreCase("Set-Cookie")) { List<String> cookies = header.getValue(); HashMap<String, HttpCookie> cookieMap = new HashMap<String, HttpCookie>(); for (String string : cookies) { List<HttpCookie> cookie = HttpCookie.parse(string); for (HttpCookie httpCookie : cookie) { if (httpCookie.getDomain() != null && httpCookie.getPath() != null) { cookieMap.put(httpCookie.getName() + httpCookie.getDomain() + httpCookie.getPath(), httpCookie); } else if (httpCookie.getDomain() == null && httpCookie.getPath() != null) { cookieMap.put(httpCookie.getName() + httpCookie.getPath(), httpCookie); } else if (httpCookie.getDomain() != null && httpCookie.getPath() == null) { cookieMap.put(httpCookie.getName() + httpCookie.getDomain(), httpCookie); } else { cookieMap.put(httpCookie.getName(), httpCookie); } } } EApplication.setCookies(cookieMap); if (EApplication.getCookies() != null) { ELog.I("?cookie", EApplication.getCookies().toString()); } } else { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } } return response; }
From source file:net.oneandone.sushi.fs.webdav.methods.Method.java
public WebdavConnection request() throws IOException { WebdavConnection conn;/* ww w . j a v a 2s. c om*/ setRequestHeader("Expires", "0"); setRequestHeader("Pragma", "no-cache"); setRequestHeader("Cache-control", "no-cache"); setRequestHeader("Cache-store", "no-store"); setRequestHeader(HTTP.USER_AGENT, "Sushi Webdav"); setContentHeader(); conn = resource.getRoot().allocate(); resource.getRoot().send(conn, request); return conn; }
From source file:org.camunda.connect.soap.httpclient.SoapHttpConnectorSystemPropertiesTest.java
@Test public void shouldSetUserAgentFromSystemProperty() { // given//from w w w . j a va2s . c o m setSystemProperty("http.agent", "foo"); SoapHttpConnector customConnector = new SoapHttpConnectorImpl(); // when customConnector.createRequest().url("http://localhost:" + PORT).payload("test").execute(); // then verify(postRequestedFor(urlEqualTo("/")).withHeader(HTTP.USER_AGENT, equalTo("foo"))); }
From source file:org.yamj.api.common.http.DefaultPoolingHttpClient.java
@Override public DigestedResponse requestContent(HttpGet httpGet, Charset charset) throws IOException { if (randomUserAgent) { httpGet.setHeader(HTTP.USER_AGENT, UserAgentSelector.randomUserAgent()); }//from w ww .ja va 2s .com try { final HttpResponse response = execute(httpGet); if (response.getEntity() == null) { httpGet.releaseConnection(); throw new IOException("No response for URI " + httpGet.getURI()); } return readContent(response, charset); } catch (IOException ioe) { httpGet.releaseConnection(); throw ioe; } }