List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.inter.trade.view.slideplayview.util.AbFileUtil.java
/** * ????.// ww w . j av a 2s .c om * @param url ? * @return ?? */ public static String getRealFileNameFromUrl(String url) { String name = null; try { if (AbStrUtil.isEmpty(url)) { return name; } URL mUrl = new URL(url); HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection(); mHttpURLConnection.setConnectTimeout(5 * 1000); mHttpURLConnection.setRequestMethod("GET"); mHttpURLConnection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); mHttpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); mHttpURLConnection.setRequestProperty("Referer", url); mHttpURLConnection.setRequestProperty("Charset", "UTF-8"); mHttpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); mHttpURLConnection.connect(); if (mHttpURLConnection.getResponseCode() == 200) { for (int i = 0;; i++) { String mine = mHttpURLConnection.getHeaderField(i); if (mine == null) { break; } if ("content-disposition".equals(mHttpURLConnection.getHeaderFieldKey(i).toLowerCase())) { Matcher m = Pattern.compile(".*filename=(.*)").matcher(mine.toLowerCase()); if (m.find()) return m.group(1).replace("\"", ""); } } } } catch (Exception e) { e.printStackTrace(); } return name; }
From source file:de.femodeling.e4.server.internal.context.HttpInvokerRequestExecutor.java
/** {@inheritDoc} */ @Override//ww w. j av a 2 s .co m protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException { super.prepareConnection(connection, contentLength); connection.setRequestProperty(IContextProvider.USER_ID, userId); connection.setRequestProperty(IContextProvider.SESSION_ID, sessionId); /*connection.setRequestProperty(IContextProvider.VERSION, Activator .getDefault().getBundle().getHeaders().get("Bundle-Version") .toString());*/ connection.setRequestProperty(IContextProvider.VERSION, "001"); //Connection Options /* -Dorg.eclipse.ecf.provider.filetransfer.excludeContributors=org.eclipse.ecf.provider.filetransfer.httpclient -Dhttp.proxySet=true -Dhttp.proxyPort=3140 -Dhttp.proxyHost=23.49.10.12 -Dhttp.proxyUser=porscheLocalUserDatabase\p323518 -Dhttp.proxyPassword=BZQEA2ZJ -Dhttp.nonProxyHosts="localhost|127.0.0.1" -Dosgi.requiredJavaVersion=1.5 -XX:MaxPermSize=256m -Xms40m -Xmx512m * */ }
From source file:org.csware.ee.utils.Tools.java
public static byte[] getByteArray(String path, String cookie) { try {/*from w ww .j a va2 s . c om*/ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(6000); conn.setReadTimeout(6000); conn.setRequestMethod("GET"); // conn.setRequestProperty("Cookie",cookie); conn.setRequestProperty("Connection", cookie); conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch"); conn.setRequestProperty("Cache-Control", "max-age=0"); conn.setRequestProperty("Content-Type", "text/html;charset=UTF-8"); conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8"); conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); // conn.setRequestProperty("Host", "pbank.95559.com.cn"); // conn.setRequestProperty("Origin", "https://creditcardapp.bankcomm.com"); // conn.setRequestProperty("Referer", "https://pbank.95559.com.cn/personbank/credit/pb0525_apply_result_qry_req.jsp"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"); if (conn.getResponseCode() == 200) { InputStream inputStream = conn.getInputStream(); return streamToByte(inputStream); } else { //Log.i("ToolsImg",conn.getResponseCode()+""); //Log.i("ToolsImg",conn.getResponseMessage()+""); } } catch (Exception e) { e.printStackTrace(); return null; } return null; }
From source file:org.apache.olingo.odata2.fit.basic.FitLoadTest.java
@Test public void useJavaHttpClient() throws IOException { final URI uri = URI.create(getEndpoint().toString() + "$metadata"); for (int i = 0; i < LOOP_COUNT; i++) { HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty(HttpHeaders.ACCEPT, HttpContentType.WILDCARD); connection.connect();/* ww w . j a v a 2 s . c o m*/ assertEquals(HttpStatusCodes.OK.getStatusCode(), connection.getResponseCode()); assertEquals(HttpContentType.APPLICATION_XML_UTF8, connection.getContentType()); } }
From source file:com.konakart.actions.gateways.GlobalCollectBaseAction.java
/** * Add things specific to GlobalCollect to the connection *///ww w . j ava 2s. c om protected void customizeConnection(HttpURLConnection connection, PaymentDetailsIf pd, List<NameValueIf> paramList) { connection.setRequestProperty("content-type", "text/xml; charset=utf-8"); }
From source file:net.kevxu.muzei.interfacelift.InterfaceliftMacdropsClient.java
protected String fetchPlainText(String query) throws IOException { URL url = new URL(query); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", getUserAgent()); try {//from w ww. j ava 2 s .co m InputStream in = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } return new String(out.toByteArray(), "UTF-8"); } finally { connection.disconnect(); } }
From source file:babel.util.language.GoogleLangDetector.java
/** * Forms an HTTP request, sends it using GET method and returns the result of * the request as a JSONObject.//from ww w . j ava2s . c om * * @param url the URL to query for a JSONObject. */ protected JSONObject retrieveJSON(final URL url) throws Exception { try { final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("referer", m_referrer); uc.setRequestMethod("GET"); uc.setDoOutput(true); try { final String result = inputStreamToString(uc.getInputStream()); return new JSONObject(result); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) { uc.getErrorStream().close(); } } } catch (Exception ex) { throw new Exception("Error retrieving detection result : " + ex.toString(), ex); } }
From source file:Transcriber.java
public String process(ByteArrayOutputStream wav) throws Exception { // Create connection URL url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "audio/l16; rate=16000"); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"); //connection.setUseCaches(false); connection.setDoOutput(true);/*from w w w . j a va 2 s .co m*/ // Write wav to request body OutputStream out = connection.getOutputStream(); //InputStream in = new FileInputStream("hello.wav"); wav.writeTo(out); out.close(); //Get Response BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; String ignore = in.readLine(); // skip first empty results line //System.out.println("ignore = " + ignore); StringBuilder responseData = new StringBuilder(); while ((line = in.readLine()) != null) { responseData.append(line); } String result = responseData.toString(); if (!result.isEmpty()) { JSONObject obj = new JSONObject(result); String text = obj.getJSONArray("result").getJSONObject(0).getJSONArray("alternative").getJSONObject(0) .getString("transcript"); return text; } return ""; }
From source file:com.adguard.commons.web.UrlUtils.java
/** * Sends a POST request//from w w w .j av a 2 s .co m * * @param url URL * @param postData Post request body * @param encoding Post request body encoding * @param contentType Body content type * @param compress If true - compress bod * @param readTimeout Read timeout * @param socketTimeout Socket timeout * @return Response */ public static String postRequest(URL url, String postData, String encoding, String contentType, boolean compress, int readTimeout, int socketTimeout) { HttpURLConnection connection = null; OutputStream outputStream = null; try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); if (contentType != null) { connection.setRequestProperty("Content-Type", contentType); } if (compress) { connection.setRequestProperty("Content-Encoding", "gzip"); } connection.setConnectTimeout(socketTimeout); connection.setReadTimeout(readTimeout); connection.setDoOutput(true); connection.connect(); if (postData != null) { outputStream = connection.getOutputStream(); if (compress) { outputStream = new GZIPOutputStream(outputStream); } if (encoding != null) { IOUtils.write(postData, outputStream, encoding); } else { IOUtils.write(postData, outputStream); } if (compress) { ((GZIPOutputStream) outputStream).finish(); } else { outputStream.flush(); } } return IOUtils.toString(connection.getInputStream(), encoding); } catch (Exception ex) { LOG.error("Error posting request to {}, post data length={}\r\n", url, StringUtils.length(postData), ex); // Ignoring exception return null; } finally { IOUtils.closeQuietly(outputStream); if (connection != null) { connection.disconnect(); } } }
From source file:io.druid.server.initialization.JettyTest.java
@Test public void testExtensionAuthFilter() throws Exception { URL url = new URL("http://localhost:" + port + "/default"); HttpURLConnection get = (HttpURLConnection) url.openConnection(); get.setRequestProperty(DummyAuthFilter.AUTH_HDR, DummyAuthFilter.SECRET_USER); Assert.assertEquals(HttpServletResponse.SC_OK, get.getResponseCode()); get = (HttpURLConnection) url.openConnection(); get.setRequestProperty(DummyAuthFilter.AUTH_HDR, "hacker"); Assert.assertEquals(HttpServletResponse.SC_UNAUTHORIZED, get.getResponseCode()); }