List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.uksf.mf.core.utility.ClassNames.java
/** * Checks if connection to URL can be established * @param url url to check/*from w w w. jav a2 s.co m*/ * @return connection state * @throws IOException error */ private static boolean checkConnection(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(1500); connection.setReadTimeout(1500); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"); connection.connect(); return connection.getResponseCode() == 404; }
From source file:jmc.util.UtlFbComents.java
public static void getURLComentarios(String url, Long numComents) throws JMCException { File f = null;/*from w w w . ja va 2 s . c o m*/ try { Properties props = ConfigPropiedades.getProperties("props_config.properties"); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL ur = new URL( "http://graph.facebook.com/comments?id=" + url + "&limit=" + numComents + "&filter=stream"); HttpURLConnection cn = (HttpURLConnection) ur.openConnection(); cn.setRequestProperty("user-agent", props.getProperty("navegador")); cn.setInstanceFollowRedirects(false); cn.setUseCaches(false); cn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream())); String dirTempHtml = props.getProperty("archive_json") + "/"; File fld = new File(dirTempHtml); boolean creado = fld.exists() ? true : fld.mkdir(); if (creado) { String archFile = "archivo.json"; String archivo = dirTempHtml + archFile; String linea = null; f = new File(archivo); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); while ((linea = br.readLine()) != null) { bw.append(linea); } bw.close(); cn.disconnect(); List<Fbcoment> lfb = Convertidor.getFbcomentObjects(1l, archivo); // Fbcoment.actComents(idContenido, lfb); } else { cn.disconnect(); } } catch (IOException e) { throw new JMCException(e); } }
From source file:jmc.util.UtlFbComents.java
public static void getComentarios(Long idContenido, Long numComents) throws JMCException { File f = null;//from w ww.j a v a 2 s. co m try { Properties props = ConfigPropiedades.getProperties("props_config.properties"); CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL ur = new URL("http://graph.facebook.com/comments?id=" + Contenido.getContenido("Where cont_id = '" + idContenido + "'").get(0).getEnlaceURL() + "&limit=" + numComents + "&filter=stream"); HttpURLConnection cn = (HttpURLConnection) ur.openConnection(); cn.setRequestProperty("user-agent", props.getProperty("navegador")); cn.setInstanceFollowRedirects(false); cn.setUseCaches(false); cn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(cn.getInputStream())); String dirTempHtml = props.getProperty("archive_json") + "/"; File fld = new File(dirTempHtml); boolean creado = fld.exists() ? true : fld.mkdir(); if (creado) { String archFile = idContenido + ".json"; String archivo = dirTempHtml + archFile; String linea = null; f = new File(archivo); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); while ((linea = br.readLine()) != null) { bw.append(linea); } bw.close(); cn.disconnect(); List<Fbcoment> lfb = Convertidor.getFbcomentObjects(idContenido, archivo); Fbcoment.actComents(idContenido, lfb); } else { cn.disconnect(); } } catch (IOException e) { throw new JMCException(e); } }
From source file:dk.nsi.minlog.test.utils.TestHelper.java
public static String sendRequest(String url, String action, String docXml, boolean failOnError) throws IOException, ServiceException { URL u = new URL(url); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); uc.setDoOutput(true);/*w w w.ja va2s . co m*/ uc.setDoInput(true); uc.setRequestMethod("POST"); uc.setRequestProperty("SOAPAction", "\"" + action + "\""); uc.setRequestProperty("Content-Type", "text/xml; charset=utf-8;"); OutputStream os = uc.getOutputStream(); IOUtils.write(docXml, os, "UTF-8"); os.flush(); os.close(); InputStream is; if (uc.getResponseCode() != 200) { is = uc.getErrorStream(); } else { is = uc.getInputStream(); } String res = IOUtils.toString(is); is.close(); if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) { throw new ServiceException(res); } uc.disconnect(); return res; }
From source file:Main.java
private static HttpURLConnection addHeaders(HttpURLConnection urlConnection, Map<String, String> headers) { try {//from w w w . j av a 2 s. c o m for (Map.Entry<String, String> entry : headers.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); urlConnection.setRequestProperty(key, value); } } catch (Exception ex) { Log.e("addHeader ", ex.getMessage()); } return urlConnection; }
From source file:ilearnrw.utils.ServerHelperClass.java
public static String sendPost(String link, String urlParameters) throws Exception { String url = baseUrl + link;//from w w w . j ava2 s .co m URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api")); con.setRequestProperty("Content-Type", "application/json;charset=utf-8"); con.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF8"); wr.write(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }
From source file:Main.java
private static HttpURLConnection defineHttpsURLConnection(HttpURLConnection connection, JSONObject jsonObject) { try {/*from w w w . j ava 2 s . com*/ connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); /* Add headers to the request */ connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(jsonObject.toString().getBytes(Charset.forName("UTF-8"))); wr.flush(); wr.close(); } catch (Exception e) { e.printStackTrace(); } return connection; }
From source file:org.cytoscape.app.internal.net.server.ScreenOriginsBeforeResponseTest.java
private static HttpURLConnection connectToURL(String urlString, String method, String origin) throws Exception { final URL url = new URL(urlString); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true);/*from w ww . j a v a 2 s . c o m*/ if (origin != null) connection.setRequestProperty("Origin", origin); connection.setConnectTimeout(1000); connection.connect(); return connection; }
From source file:com.beginner.core.utils.SmsUtil.java
public static String SMS(String postData, String postUrl) { try {/*from ww w . ja 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()); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); out.write(postData); out.flush(); out.close(); //??? if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("connect failed!"); return ""; } //?? String line, result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); while ((line = in.readLine()) != null) { result += line + "\n"; } in.close(); return result; } catch (IOException e) { e.printStackTrace(System.out); } return ""; }
From source file:com.newrelic.agent.Deployments.java
static int recordDeployment(CommandLine cmd, AgentConfig config)/* 37: */ throws Exception /* 38: */ {// ww w .j av a 2 s . co m /* 39: 35 */ String appName = config.getApplicationName(); /* 40: 36 */ if (cmd.hasOption("appname")) { /* 41: 37 */ appName = cmd.getOptionValue("appname"); /* 42: */ } /* 43: 39 */ if (appName == null) { /* 44: 40 */ throw new IllegalArgumentException( "A deployment must be associated with an application. Set app_name in newrelic.yml or specify the application name with the -appname switch."); /* 45: */ } /* 46: 43 */ System.out.println("Recording a deployment for application " + appName); /* 47: */ /* 48: 45 */ String uri = "/deployments.xml"; /* 49: 46 */ String payload = getDeploymentPayload(appName, cmd); /* 50: 47 */ String protocol = "http" + (config.isSSL() ? "s" : ""); /* 51: 48 */ URL url = new URL(protocol, config.getApiHost(), config.getApiPort(), uri); /* 52: */ /* 53: 50 */ System.out.println(MessageFormat.format("Opening connection to {0}:{1}", new Object[] { config.getApiHost(), Integer.toString(config.getApiPort()) })); /* 54: */ /* 55: */ /* 56: 53 */ HttpURLConnection conn = (HttpURLConnection) url.openConnection(); /* 57: 54 */ conn.setRequestProperty("x-license-key", config.getLicenseKey()); /* 58: */ /* 59: 56 */ conn.setRequestMethod("POST"); /* 60: 57 */ conn.setConnectTimeout(10000); /* 61: 58 */ conn.setReadTimeout(10000); /* 62: 59 */ conn.setDoOutput(true); /* 63: 60 */ conn.setDoInput(true); /* 64: */ /* 65: 62 */ conn.setRequestProperty("Content-Length", Integer.toString(payload.length())); /* 66: 63 */ conn.setFixedLengthStreamingMode(payload.length()); /* 67: 64 */ conn.getOutputStream().write(payload.getBytes()); /* 68: */ /* 69: 66 */ int responseCode = conn.getResponseCode(); /* 70: 67 */ if (responseCode < 300) /* 71: */ { /* 72: 68 */ System.out.println("Deployment successfully recorded"); /* 73: */ } /* 74: 69 */ else if (responseCode == 401) /* 75: */ { /* 76: 70 */ System.out.println( "Unable to notify New Relic of the deployment because of an authorization error. Check your license key."); /* 77: 71 */ System.out.println("Response message: " + conn.getResponseMessage()); /* 78: */ } /* 79: */ else /* 80: */ { /* 81: 73 */ System.out.println("Unable to notify New Relic of the deployment"); /* 82: 74 */ System.out.println("Response message: " + conn.getResponseMessage()); /* 83: */ } /* 84: 76 */ boolean isError = responseCode >= 300; /* 85: 77 */ if ((isError) || (config.isDebugEnabled())) /* 86: */ { /* 87: 78 */ System.out.println("Response code: " + responseCode); /* 88: 79 */ InputStream inStream = isError ? conn.getErrorStream() : conn.getInputStream(); /* 89: 81 */ if (inStream != null) /* 90: */ { /* 91: 82 */ ByteArrayOutputStream output = new ByteArrayOutputStream(); /* 92: 83 */ Streams.copy(inStream, output); /* 93: */ /* 94: 85 */ PrintStream out = isError ? System.err : System.out; /* 95: */ /* 96: 87 */ out.println(output); /* 97: */ } /* 98: */ } /* 99: 90 */ return responseCode; /* 100: */ }