List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:Main.java
static void getHTTPXml(URL url) throws Exception { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("ACCEPT", "application/xml"); InputStream xml = conn.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse(xml); System.out.println(document); String doctype = conn.getContentType(); System.out.println(doctype);/* ww w. j a v a2s . c om*/ XPathFactory pathFactory = XPathFactory.newInstance(); XPath path = pathFactory.newXPath(); XPathExpression expression; expression = path.compile("/result/checkid"); NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET); String checkids[] = getNodeValue(nodeList); for (String checkid : checkids) { System.out.print(checkid + ", "); } conn.disconnect(); }
From source file:Main.java
public static String getHtml(String getUrl, String charsetName) { String html = ""; URL url;//from w w w .j a v a 2 s. c o m try { url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", userAgent); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName)); String temp = ""; while ((temp = br.readLine()) != null) { html = html + (temp + '\n'); } } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:Main.java
public static String deleteUrl(String url, Bundle params) throws MalformedURLException, IOException { System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " agent"); conn.setRequestMethod("DELETE"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(false);//from w w w .j a va 2 s. c o m conn.setDoInput(true); //conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); String response = ""; try { response = read(conn.getInputStream()); } catch (FileNotFoundException e) { // Error Stream contains JSON that we can parse to a FB error response = read(conn.getErrorStream()); } return response; }
From source file:Main.java
public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); // determine the image size and allocate a buffer int fileSize = connection.getContentLength(); byte[] imageData = new byte[fileSize]; // download the file Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")"); if (fileSize > 0) { BufferedInputStream istream = new BufferedInputStream(connection.getInputStream()); int bytesRead = 0; int offset = 0; while (bytesRead != -1 && offset < fileSize) { bytesRead = istream.read(imageData, offset, fileSize - offset); offset += bytesRead;//from w ww. j a v a2 s. com } istream.close(); } else Log.d(TAG, "fileSize is 0! skipping"); // clean up connection.disconnect(); return imageData; }
From source file:Main.java
static String downloadUrl(String myurl) throws IOException { InputStream is = null;//from ww w . j a v a 2 s . co m try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Authorization", "Basic cm9vdDpvcmllbnRkYg=="); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); //start conn.connect(); int response = conn.getResponseCode(); Log.e("The response is: ", "" + response); is = conn.getInputStream(); //converte inputStream in stringa String contentAsString = readIt(is); return contentAsString; } catch (IOException e) { Log.e("HTTP", e.getMessage()); return null; } finally { if (is != null) { is.close(); } } }
From source file:Main.java
static String downloadHtml(String urlString) { StringBuffer buffer = new StringBuffer(); try {/*from www . java 2 s . co m*/ URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); String encoding = conn.getContentEncoding(); InputStream inStr = null; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); } int ptr = 0; InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312")); while ((ptr = inStrReader.read()) != -1) { buffer.append((char) ptr); } inStrReader.close(); conn.disconnect(); inStr.close(); } catch (Exception e) { e.printStackTrace(); } return buffer.toString(); }
From source file:Main.java
public static String getHtml(String getUrl, int outtime, String charsetName) { String html = ""; URL url;//from w ww. ja v a 2 s.com try { url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; CIBA)"); // connection.setRequestProperty("Connection", "Keep-Alive"); // connection.setRequestProperty("Cache-Control", "no-cache"); connection.setConnectTimeout(outtime); connection.connect(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName)); String temp = ""; while ((temp = br.readLine()) != null) { html = html + (temp + '\n'); } try { br.close(); } catch (Exception e) { // TODO: handle exception } try { connection.disconnect(); } catch (Exception e) { // TODO: handle exception } } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:Main.java
public static HttpURLConnection buildConnection(String url, String requestMethod) throws IOException { HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); /*set request*/ con.setRequestMethod(requestMethod); con.setRequestProperty("Accept-Language", "UTF-8"); con.setDoOutput(true);/*from www .j a v a2 s .c om*/ return con; }
From source file:me.bramhaag.discordselfbot.util.Util.java
/** * Get image from URL.//from www . j a v a2 s .c o m * @param url url to get image from. * @return image. * * @throws IOException if an I/O error occurs while creating the input stream. */ @NonNull public static BufferedImage getImage(@NonNull String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", Constants.USER_AGENT); return ImageIO.read(connection.getInputStream()); }
From source file:Main.java
public static byte[] getBytes(String url) { try {//from w ww . java2 s . c om URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Charset", "UTF-8"); InputStream is = connection.getInputStream(); int contentLength = connection.getContentLength(); byte[] data = new byte[contentLength]; int alreadyRead = 0; int len = 0; while (alreadyRead < contentLength) { len = is.read(data, alreadyRead, contentLength - alreadyRead); alreadyRead += len; } is.close(); return data; } catch (Exception e) { e.printStackTrace(); return null; } }