List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:com.amastigote.xdu.query.module.EduSystem.java
private void preLogin() throws IOException { URL url = new URL(SYS_HOST); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.connect(); List<String> cookies_to_set_a = httpURLConnection.getHeaderFields().get("Set-Cookie"); for (String e : cookies_to_set_a) if (e.contains("JSESSIONID=")) SYS_JSESSIONID = e.substring(e.indexOf("JSESSIONID=") + 11, e.indexOf(";")); httpURLConnection.disconnect();/* w ww.j a va 2s. c om*/ httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.connect(); List<String> cookies_to_set = httpURLConnection.getHeaderFields().get("Set-Cookie"); for (String e : cookies_to_set) { if (e.contains("route=")) ROUTE = e.substring(6); else if (e.contains("JSESSIONID=")) LOGIN_JSESSIONID = e.substring(11, e.indexOf(";")); else if (e.contains("BIGipServeridsnew.xidian.edu.cn=")) BIGIP_SERVER_IDS_NEW = e.substring(32, e.indexOf(";")); } BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream())); String html = ""; String temp; while ((temp = bufferedReader.readLine()) != null) { html += temp; } Document document = Jsoup.parse(html); Elements elements = document.select("input[type=hidden]"); for (Element element : elements) { switch (element.attr("name")) { case "lt": LOGIN_PARAM_lt = element.attr("value"); break; case "execution": LOGIN_PARAM_execution = element.attr("value"); break; case "_eventId": LOGIN_PARAM__eventId = element.attr("value"); break; case "rmShown": LOGIN_PARAM_rmShown = element.attr("value"); break; } } }
From source file:com.jeffrodriguez.webtools.client.URLConnectionWebClientImpl.java
@Override public Map<String, List<String>> getHeaders(String url) throws IOException { logger.log(Level.INFO, "Getting HEAD from {0}", url); // Build and connect HttpURLConnection connection = buildConnection(url); connection.setDoInput(true);//from w w w .j av a 2 s . co m connection.setRequestMethod("HEAD"); connection.connect(); // Get the headers return disconnectAndReturn(connection, connection.getHeaderFields()); }
From source file:com.amastigote.xdu.query.module.EduSystem.java
private @Nullable JSONObject personalInfoQuery() throws IOException, JSONException { if (!checkIsLogin(ID)) { return null; }/*ww w . j a v a 2 s .c o m*/ URL url = new URL(SYS_HOST + "xjInfoAction.do?oper=xjxx"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Cookie", "JSESSIONID=" + SYS_JSESSIONID); httpURLConnection.connect(); Document document = Jsoup.parse(httpURLConnection.getInputStream(), "gb2312", httpURLConnection.getURL().toString()); document = Jsoup.parse(document.toString().replaceAll(" ", "")); Elements elements1 = document.select("td[width=275]"); JSONObject jsonObject = new JSONObject(); jsonObject.put(StudentKey.ID, elements1.get(0).text()); jsonObject.put(StudentKey.NAME, elements1.get(1).text()); jsonObject.put(StudentKey.GENDER, elements1.get(6).text()); jsonObject.put(StudentKey.NATION, elements1.get(10).text()); jsonObject.put(StudentKey.NATIVE_PLACE, elements1.get(11).text()); jsonObject.put(StudentKey.DEPARTMENT, elements1.get(24).text()); jsonObject.put(StudentKey.MAJOR, elements1.get(25).text()); jsonObject.put(StudentKey.CLASS, elements1.get(28).text()); return jsonObject; }
From source file:com.permpings.utils.facebook.sdk.Util.java
/** * Connect to an HTTP URL and return the response as a string. * * Note that the HTTP method override is used on non-GET requests. (i.e. * requests are made as "POST" with method specified in the body). * * @param url - the resource to open: must be a welformed URL * @param method - the HTTP method to use ("GET", "POST", etc.) * @param params - the query parameter for the URL (e.g. access_token=foo) * @return the URL contents as a String/* www . j av a 2 s.c om*/ * @throws MalformedURLException - if the URL format is invalid * @throws IOException - if a network problem occurs */ public static String openUrl(String url, String method, Bundle params) throws MalformedURLException, IOException { // random string as boundary for multi-part http post String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; String endLine = "\r\n"; OutputStream os; if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } Util.logd("Facebook-Util", method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); if (!method.equals("GET")) { Bundle dataparams = new Bundle(); for (String key : params.keySet()) { Object parameter = params.get(key); if (parameter instanceof byte[]) { dataparams.putByteArray(key, (byte[]) parameter); } } // use method override if (!params.containsKey("method")) { params.putString("method", method); } if (params.containsKey("access_token")) { String decoded_token = URLDecoder.decode(params.getString("access_token")); params.putString("access_token", decoded_token); } conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); os.write(("--" + strBoundary + endLine).getBytes()); os.write((encodePostBody(params, strBoundary)).getBytes()); os.write((endLine + "--" + strBoundary + endLine).getBytes()); if (!dataparams.isEmpty()) { for (String key : dataparams.keySet()) { os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes()); os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes()); os.write(dataparams.getByteArray(key)); os.write((endLine + "--" + strBoundary + endLine).getBytes()); } } os.flush(); } 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:com.ble.facebook.Util.java
public static String posttowall(String url, String method, Bundle params, String Accesstoken) throws MalformedURLException, IOException { // random string as boundary for multi-part http post String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; String endLine = "\r\n"; OutputStream os;/*from ww w.ja va2s .c o m*/ if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } Log.d("Facebook-Util", method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); if (!method.equals("GET")) { Bundle dataparams = new Bundle(); for (String key : params.keySet()) { if (params.getByteArray(key) != null) { dataparams.putByteArray(key, params.getByteArray(key)); } } // use method override if (!params.containsKey("method")) { params.putByteArray("method", method.getBytes()); } if (params.containsKey("access_token")) { String decoded_token = URLDecoder.decode(Accesstoken); params.putByteArray("access_token", decoded_token.getBytes()); } conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); Log.d("OutputStreamedData", ("--" + strBoundary + endLine) + (encodePostBody(params, strBoundary)) + (endLine + "--" + strBoundary + endLine)); os.write(("--" + strBoundary + endLine).getBytes()); os.write((encodePostBody(params, strBoundary)).getBytes()); os.write((endLine + "--" + strBoundary + endLine).getBytes()); if (!dataparams.isEmpty()) { for (String key : dataparams.keySet()) { os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes()); os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes()); os.write(dataparams.getByteArray(key)); os.write((endLine + "--" + strBoundary + endLine).getBytes()); } } os.flush(); } 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:karroo.app.test.facebook.Util.java
/** * Connect to an HTTP URL and return the response as a string. * * Note that the HTTP method override is used on non-GET requests. (i.e. * requests are made as "POST" with method specified in the body). * * @param url - the resource to open: must be a welformed URL * @param method - the HTTP method to use ("GET", "POST", etc.) * @param params - the query parameter for the URL (e.g. access_token=foo) * @return the URL contents as a String//w w w . ja va 2s.c o m * @throws MalformedURLException - if the URL format is invalid * @throws IOException - if a network problem occurs */ public static String openUrl(String url, String method, Bundle params) throws MalformedURLException, IOException { // random string as boundary for multi-part http post String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; String endLine = "\r\n"; OutputStream os; if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } Log.d("Facebook-Util", method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); if (!method.equals("GET")) { Bundle dataparams = new Bundle(); for (String key : params.keySet()) { // if (params.getByteArray(key) != null) { if (params.get(key) instanceof byte[]) { dataparams.putByteArray(key, params.getByteArray(key)); } } // use method override if (!params.containsKey("method")) { params.putString("method", method); } if (params.containsKey("access_token")) { String decoded_token = URLDecoder.decode(params.getString("access_token")); params.putString("access_token", decoded_token); } conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); os.write(("--" + strBoundary + endLine).getBytes()); os.write((encodePostBody(params, strBoundary)).getBytes()); os.write((endLine + "--" + strBoundary + endLine).getBytes()); if (!dataparams.isEmpty()) { for (String key : dataparams.keySet()) { os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes()); os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes()); os.write(dataparams.getByteArray(key)); os.write((endLine + "--" + strBoundary + endLine).getBytes()); } } os.flush(); } 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:com.mobli.android.Util.java
/** * Connect to an HTTP URL and return the response as a string. * //from w w w .j av a 2s .c om * Note that the HTTP method override is used on non-GET requests. (i.e. * requests are made as "POST" with method specified in the body). * * @param url * - the resource to open: must be a welformed URL * @param method * - the HTTP method to use ("GET", "POST", etc.) * @param params * - the query parameter for the URL (e.g. access_token=foo) * @return the URL contents as a String * @throws MalformedURLException * - if the URL format is invalid * @throws IOException * - if a network problem occurs */ public static String openUrl(String url, String method, Bundle params) throws MalformedURLException, IOException { // random string as boundary for multi-part http post String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; String endLine = "\r\n"; OutputStream os; if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } Util.logd("Mobli-Util", method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " MobliAndroidSDK"); if (!method.equals("GET")) { Bundle dataparams = new Bundle(); for (String key : params.keySet()) { Object parameter = params.get(key); if (parameter instanceof byte[]) { dataparams.putByteArray(key, (byte[]) parameter); } } // use method override if (!params.containsKey("method")) { params.putString("method", method); } if (params.containsKey("access_token")) { String decoded_token = URLDecoder.decode(params.getString("access_token")); params.putString("access_token", decoded_token); } conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); os.write(("--" + strBoundary + endLine).getBytes()); os.write((encodePostBody(params, strBoundary)).getBytes()); os.write((endLine + "--" + strBoundary + endLine).getBytes()); if (!dataparams.isEmpty()) { for (String key : dataparams.keySet()) { os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes()); os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes()); os.write(dataparams.getByteArray(key)); os.write((endLine + "--" + strBoundary + endLine).getBytes()); } } os.flush(); } String response = ""; try { response = read(conn.getInputStream()); } catch (FileNotFoundException e) { // Error Stream contains JSON that we can parse to a Mobli error response = read(conn.getErrorStream()); } return response; }
From source file:com.amastigote.xdu.query.module.EduSystem.java
private @Nullable JSONObject gradesQuery() throws IOException, JSONException { if (!checkIsLogin(ID)) { return null; }/*from w w w .j av a 2s.c o m*/ URL url = new URL(SYS_HOST + GRADE_QUERY_SUFFIX); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("Cookie", "JSESSIONID=" + SYS_JSESSIONID); httpURLConnection.connect(); Document document = Jsoup.parse(httpURLConnection.getInputStream(), "gb2312", httpURLConnection.getURL().toString()); document = Jsoup.parse(document.toString().replaceAll(" ", "")); JSONObject jsonObject = new JSONObject(); Elements elements_content = document.select("td[class=pageAlign]"); Elements elements_titles = document.select("b"); for (int i = 0; i < elements_titles.size(); i++) { JSONObject jsonObject_semester = new JSONObject(); String semester_key = elements_titles.get(i).text().trim(); Element table_for_this_semester = elements_content.get(i); Elements elements_rows = table_for_this_semester.select("td[align=center]"); for (int j = 0; j < elements_rows.size() / 7; j++) { JSONObject jsonObject_course = new JSONObject(); String course_key = elements_rows.get(j * 7 + 2).text().trim(); jsonObject_course.put(GradeKey.ID, elements_rows.get(j * 7).text().trim()); jsonObject_course.put(GradeKey.CREDIT, elements_rows.get(j * 7 + 4).text().trim()); jsonObject_course.put(GradeKey.ATTR, elements_rows.get(j * 7 + 5).text().trim()); jsonObject_course.put(GradeKey.GRADE, elements_rows.get(j * 7 + 6).text().trim()); jsonObject_semester.put(course_key, jsonObject_course); } jsonObject.put(semester_key, jsonObject_semester); } return jsonObject; }
From source file:com.mercandalli.android.apps.files.common.net.TaskGet.java
@Override protected String doInBackground(Void... urls) { try {//from ww w . ja v a 2 s. c om if (this.parameters != null) { if (!StringUtils.isNullOrEmpty(Config.getNotificationId())) { parameters.add(new StringPair("android_id", "" + Config.getNotificationId())); } url = NetUtils.addUrlParameters(url, parameters); } Log.d("TaskGet", "url = " + url); URL tmp_url = new URL(url); HttpURLConnection conn = (HttpURLConnection) tmp_url.openConnection(); conn.setReadTimeout(10_000); conn.setConnectTimeout(15_000); conn.setRequestMethod("GET"); if (isAuthentication) { conn.setRequestProperty("Authorization", "Basic " + Config.getUserToken()); } conn.setUseCaches(false); conn.setDoInput(true); conn.connect(); // Starts the query int responseCode = conn.getResponseCode(); InputStream inputStream = new BufferedInputStream(conn.getInputStream()); // convert inputstream to string String resultString = convertInputStreamToString(inputStream); //int responseCode = response.getStatusLine().getStatusCode(); if (responseCode >= 300) { resultString = "Status Code " + responseCode + ". " + resultString; } conn.disconnect(); return resultString; } catch (IOException e) { Log.e(getClass().getName(), "IOException", e); } return null; }