List of usage examples for java.net HttpURLConnection HTTP_OK
int HTTP_OK
To view the source code for java.net HttpURLConnection HTTP_OK.
Click Source Link
From source file:de.mg.stock.server.util.HttpUtil.java
public String get(String urlStr) { try {/*from w ww . j av a 2 s . c om*/ URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream response = connection.getInputStream(); return IOUtils.toString(response); } else { log.log(Level.WARNING, "retrieving " + urlStr + " returned " + connection.getResponseCode()); return null; } } catch (IOException e) { log.log(Level.WARNING, "retrieving " + urlStr, e); return null; } }
From source file:WebCategorizer.java
private JSONObject fetchCategories() { try {/*from ww w . j av a 2s. c o m*/ JSONObject webCatList = new JSONObject(); URL obj = new URL(categoriesUrl); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-agent", "Web Categorizer"); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONArray inputData = new JSONArray(response.toString()); for (Object currObj : inputData) { JSONObject tempObj = (JSONObject) currObj; webCatList.put(String.format("%02X", tempObj.get("num")).toLowerCase(), tempObj.get("name")); } FileWriter file = new FileWriter(categoriesFile); file.write(webCatList.toString()); file.flush(); file.close(); } else { System.out.println("Not working"); } return webCatList; } catch (Exception ex) { return new JSONObject(); } }
From source file:com.adrguides.utils.HTTPUtils.java
public static InputStream openAddress(Context context, URL url) throws IOException { if ("file".equals(url.getProtocol()) && url.getPath().startsWith("/android_asset/")) { return context.getAssets().open(url.getPath().substring(15)); // "/android_asset/".length() == 15 } else {/*from w ww .j a va 2 s . co m*/ URLConnection urlconn = url.openConnection(); urlconn.setReadTimeout(10000 /* milliseconds */); urlconn.setConnectTimeout(15000 /* milliseconds */); urlconn.setAllowUserInteraction(false); urlconn.setDoInput(true); urlconn.setDoOutput(false); if (urlconn instanceof HttpURLConnection) { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responsecode = connection.getResponseCode(); if (responsecode != HttpURLConnection.HTTP_OK) { throw new IOException("Http response code returned:" + responsecode); } } return urlconn.getInputStream(); } }
From source file:fr.ms.tomcat.manager.TomcatManagerUrl.java
public static String appelUrl(final URL url, final String username, final String password, final String charset) { try {/* w w w . ja va 2s.co m*/ final URLConnection urlTomcatConnection = url.openConnection(); final HttpURLConnection connection = (HttpURLConnection) urlTomcatConnection; LOG.debug("url : " + url); connection.setAllowUserInteraction(false); connection.setDoInput(true); connection.setUseCaches(false); connection.setDoOutput(false); connection.setRequestMethod("GET"); connection.setRequestProperty("Authorization", toAuthorization(username, password)); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { throw new TomcatManagerException("Reponse http : " + connection.getResponseMessage() + " - Les droits \"manager\" ne sont pas appliques - utilisateur : \"" + username + "\" password : \"" + password + "\""); } throw new TomcatManagerException("HttpURLConnection.HTTP_UNAUTHORIZED"); } final String response = toString(connection.getInputStream(), charset); LOG.debug("reponse : " + response); return response; } catch (final Exception e) { throw new TomcatManagerException("L'url du manager tomcat est peut etre incorrecte : \"" + url + "\"", e); } }
From source file:org.angellist.angellistmobile.ApiCalls.java
private static String GetData(String url) { byte[] result = null; String str = ""; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); try {/*from w w w . j a v a2 s .c om*/ HttpResponse response = client.execute(get); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) { result = EntityUtils.toByteArray(response.getEntity()); str = new String(result, "UTF-8"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:com.adr.raspberryleds.HTTPUtils.java
public static JSONObject execPOST(String address, JSONObject params) throws IOException { BufferedReader readerin = null; Writer writerout = null;/*from w w w .java 2 s.c o m*/ try { URL url = new URL(address); String query = params.toString(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setReadTimeout(10000 /* milliseconds */); connection.setConnectTimeout(15000 /* milliseconds */); connection.setRequestMethod("POST"); connection.setAllowUserInteraction(false); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.addRequestProperty("Content-Type", "application/json,encoding=UTF-8"); connection.addRequestProperty("Content-length", String.valueOf(query.length())); writerout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); writerout.write(query); writerout.flush(); writerout.close(); writerout = null; int responsecode = connection.getResponseCode(); if (responsecode == HttpURLConnection.HTTP_OK) { StringBuilder text = new StringBuilder(); readerin = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line = null; while ((line = readerin.readLine()) != null) { text.append(line); text.append(System.getProperty("line.separator")); } JSONObject result = new JSONObject(text.toString()); if (result.has("exception")) { throw new IOException( MessageFormat.format("Remote exception: {0}.", result.getString("exception"))); } else { return result; } } else { throw new IOException(MessageFormat.format("HTTP response error: {0}. {1}", Integer.toString(responsecode), connection.getResponseMessage())); } } catch (JSONException ex) { throw new IOException(MessageFormat.format("Parse exception: {0}.", ex.getMessage())); } finally { if (writerout != null) { writerout.close(); writerout = null; } if (readerin != null) { readerin.close(); readerin = null; } } }
From source file:com.fusesource.test.http.ResourceHttpHandler.java
@Override public void handle(HttpExchange exchange) throws IOException { exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); exchange.getResponseBody().write(response); exchange.close();/*from w ww . j a v a 2 s .co m*/ }
From source file:com.arjuna.qa.junit.HttpUtils.java
/** Perform a get on the indicated URL and assert an HTTP_OK response code * * @param url/*from ww w . j ava 2 s . co m*/ * @return The commons HttpClient used to perform the get * @throws Exception on any failure */ public static HttpMethodBase accessURL(URL url) throws Exception { return accessURL(url, null, HttpURLConnection.HTTP_OK); }
From source file:com.jyzn.wifi.validate.platforminterface.SmsInterfaceImpl.java
@Override public Map HttpSendSms(String postUrl, String postData) { String result = ""; Map resultMap = Maps.newHashMap(); try {// ww w. j a v a2s .c o m //??POST URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setUseCaches(false); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", "" + postData.length()); try { OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(postData); out.flush();//? } catch (IOException e) { } //??? if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("connect failed!"); resultMap.put("status", "fail"); //return "fail"; } //?? String line; try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) { while ((line = in.readLine()) != null) { result += line + "\n"; } } resultMap.put("status", "sucess"); resultMap.put("result", result); } catch (IOException e) { e.printStackTrace(System.out); } return resultMap; }
From source file:com.uk_postcodes.api.Postcode.java
/** * Get the postcode closest to the location. * @param location The device's location * @return the closest postcode/*from ww w. java 2 s . c o m*/ * @throws Exception */ public static String forLocation(Location location) throws Exception { String address = String.format(CALL, location.getLatitude(), location.getLongitude()); Log.d("Postcode", "Calling: " + address); URL callUrl = new URL(address); HttpURLConnection callConnection = (HttpURLConnection) callUrl.openConnection(); // The ten-second rule: // If there's no data in 10s (or TIMEOUT), assume the worst. callConnection.setReadTimeout(10000); // Set the request method to GET. callConnection.setRequestMethod("GET"); int code = callConnection.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { throw new Exception("A non-200 code was returned: " + code); } String responseStr; responseStr = IOUtils.toString(callConnection.getInputStream()); callConnection.disconnect(); JsonParser jp = new JsonParser(); JsonElement response = jp.parse(responseStr); return response.getAsJsonObject().get("postcode").getAsString(); }