List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:Store.AfricasTalkingGateway.java
private String sendPOSTRequest(HashMap<String, String> dataMap_, String urlString_) throws Exception { try {/*from w w w . ja va 2 s . c om*/ String data = new String(); Iterator<Map.Entry<String, String>> it = dataMap_.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next(); data += URLEncoder.encode(pairs.getKey().toString(), "UTF-8"); data += "=" + URLEncoder.encode(pairs.getValue().toString(), "UTF-8"); if (it.hasNext()) data += "&"; } URL url = new URL(urlString_); URLConnection conn = url.openConnection(); conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("apikey", _apiKey); conn.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(data); writer.flush(); HttpURLConnection http_conn = (HttpURLConnection) conn; responseCode = http_conn.getResponseCode(); BufferedReader reader; if (responseCode == HTTP_CODE_OK || responseCode == HTTP_CODE_CREATED) reader = new BufferedReader(new InputStreamReader(http_conn.getInputStream())); else reader = new BufferedReader(new InputStreamReader(http_conn.getErrorStream())); String response = reader.readLine(); if (DEBUG) System.out.println(response); reader.close(); return response; } catch (Exception e) { throw e; } }
From source file:net.naonedbus.rest.controller.RestController.java
/** * Lire un flux Json./*ww w . jav a2s. c om*/ * * @param url * L'url * @return Le fulx Json au format string * @throws IOException */ protected String readJsonFromUrl(final URL url) throws IOException { if (DBG) Log.d(LOG_TAG, "readJsonFromUrl " + url.toString()); final URLConnection conn = url.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); conn.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage()); final InputStreamReader comReader = new InputStreamReader(conn.getInputStream()); final String source = IOUtils.toString(comReader); IOUtils.closeQuietly(comReader); return source; }
From source file:org.ohie.pocdemo.form.util.InfoMan.java
private String invoke(final String req) throws Exception { log.info("Sending to " + URL + "\n" + req); final URLConnection ucon = new URL(URL).openConnection(); ucon.setRequestProperty("Accept", "text/xml"); ucon.setRequestProperty("Accept-Charset", "utf-8"); ucon.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); String userPassword = USERNAME + ":" + PASSWORD; String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); ucon.setRequestProperty("Authorization", "Basic " + encoding); ucon.setDoInput(true);//from w w w .j a v a 2 s . c om ucon.setDoOutput(true); ucon.getOutputStream().write(req.getBytes()); final InputStream in = Util.getRawStream(ucon); final String rsp; try { rsp = Util.readStream(in); } finally { in.close(); } log.info("Received from " + URL + "\n" + rsp); return rsp; }
From source file:be.apsu.extremon.probes.tsp.TSPProbe.java
private TimeStampResponse probe(TimeStampRequest request) throws IOException, TSPException { URLConnection connection = this.url.openConnection(); connection.setDoInput(true);//from ww w .ja v a 2s . co m connection.setDoOutput(true); connection.setUseCaches(false); connection.setRequestProperty("Content-Type", "application/timestamp-query"); OutputStream outputStream = (connection.getOutputStream()); outputStream.write(request.getEncoded()); outputStream.flush(); outputStream.close(); InputStream inputStream = connection.getInputStream(); TimeStampResponse response = new TimeStampResponse(inputStream); inputStream.close(); return response; }
From source file:com.github.beat.signer.pdf_signer.TSAClient.java
private byte[] getTSAResponse(byte[] request) throws IOException { LOGGER.debug("Opening connection to TSA server"); // FIXME: support proxy servers URLConnection connection = tsaInfo.getTsaUrl().openConnection(); connection.setDoOutput(true);/*from www . ja v a 2 s .c o m*/ connection.setDoInput(true); connection.setReadTimeout(CONNECT_TIMEOUT); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setRequestProperty("Content-Type", "application/timestamp-query"); // TODO set accept header LOGGER.debug("Established connection to TSA server"); String username = tsaInfo.getUsername(); char[] password = tsaInfo.getPassword(); if (StringUtils.isNotBlank(username) && password != null) { // FIXME this most likely wrong, e.g. set correct request property! // connection.setRequestProperty(username, password); } // read response sendRequest(request, connection); LOGGER.debug("Waiting for response from TSA server"); byte[] response = getResponse(connection); LOGGER.debug("Received response from TSA server"); return response; }
From source file:game.Clue.JerseyClient.java
public void sendPUT(String name) { System.out.println("SendPUT method called"); try {/*from w w w.jav a2 s . c o m*/ JSONObject jsonObject = new JSONObject("{name:" + name + "}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/player1"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("PUT", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:game.Clue.JerseyClient.java
public void sendPOST() { System.out.println("POST method called"); try {/* w w w .j a v a 2s . c o m*/ JSONObject jsonObject = new JSONObject("{player:Brian}"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); //setDoOutput(true); connection.setRequestProperty("POST", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); System.out.println(jsonObject.toString()); out.write("{" + jsonObject.toString()); System.out.println("Sent PUT message to server"); out.close(); } catch (Exception e) { System.out.println("\nError while calling REST POST Service"); System.out.println(e); } }
From source file:game.Clue.JerseyClient.java
public void isgameReady() { //Check to see if all players have joined game try {// ww w . j a v a 2 s . c o m // URL url = new URL("http://192.168.1.7:8080/CluelessServer/webresources/service/game/Status"); URL url = new URL( "http://ec2-54-165-198-60.compute-1.amazonaws.com:8080/CluelessServer/webresources/service/game/"); URLConnection connection = url.openConnection(); connection.setDoInput(true); //setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // while (in.readLine() != null) { //} System.out.println("\nGET Request for :" + "Game Ready Status? " + "Sent"); System.out.print(in.readLine()); //close connection in.close(); } catch (Exception e) { System.out.println("\nError while calling REST Get Service"); System.out.println(e); } }
From source file:com.yglab.openapi.youtube.caption.Video.java
public InputStreamReader readURL(String s) throws MalformedURLException, IOException { URL url;//from w w w .ja va 2 s . com InputStreamReader isr; String appName, appVersion; appName = "ScanNews"; appVersion = "1.0.0"; url = new URL(s); URLConnection urlconn = url.openConnection(); urlconn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); urlconn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; " + appName + "/" + appVersion + ")"); urlconn.connect(); isr = new InputStreamReader(urlconn.getInputStream(), "UTF-8"); return isr; }
From source file:org.safecreative.api.SafeCreativeAPI.java
public String call(String params) { String uri = baseUrl + API_ENDPOINT; OutputStream os = null;/* w w w .java 2 s. c om*/ String response = null; try { log.debug(String.format("api request: \n%s?%s\n", uri, params)); URL url = new URL(uri); URLConnection conn = url.openConnection(); conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=" + DEFAULT_ENCODING); conn.setDoOutput(true); conn.setUseCaches(false); os = conn.getOutputStream(); os.write(params.getBytes(DEFAULT_ENCODING)); response = readString(conn.getInputStream()); log.debug(String.format("api response:\n %s\n", response)); if (isError(response) && INVALID_TIME_ERROR.equals(getErrorCode(response))) { log.warn("Client time needs resyncing"); timeOffset = null; } return response; } catch (Throwable e) { throw new RuntimeException(ApiException.wrap(e, uri + "?" + params, response)); } finally { IOHelper.closeQuietly(os); } }