List of usage examples for java.net HttpURLConnection setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
From source file:crow.weibo.util.WeiboUtil.java
public static String multipartPost(HttpURLConnection conn, List<PostParameter> params) throws IOException { OutputStream os;//from w ww .java2 s .c o m List<PostParameter> dataparams = new ArrayList<PostParameter>(); for (PostParameter key : params) { if (key.isFile()) { dataparams.add(key); } } String BOUNDARY = Util.md5(String.valueOf(System.currentTimeMillis())); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY); ByteArrayBuffer buff = new ByteArrayBuffer(1000); for (PostParameter p : params) { byte[] arr = p.toMultipartByte(BOUNDARY, "UTF-8"); buff.append(arr, 0, arr.length); } String end = "--" + BOUNDARY + "--" + "\r\n"; byte[] endArr = end.getBytes(); buff.append(endArr, 0, endArr.length); conn.setRequestProperty("Content-Length", buff.length() + ""); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); os.write(buff.toByteArray()); buff.clear(); os.flush(); String response = ""; response = Util.inputStreamToString(conn.getInputStream()); return response; }
From source file:com.depas.utils.FileUtils.java
public static boolean fileExistByURL(String urlName) { try {// www.j a v a 2 s . c o m HttpURLConnection.setFollowRedirects(false); // note : you may also need // HttpURLConnection.setInstanceFollowRedirects(false) HttpURLConnection con = (HttpURLConnection) new URL(urlName).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { logger.warn("Error checking if a file exists by URL [URLName=" + urlName + "]: " + e, e); return false; } }
From source file:com.iStudy.Study.Renren.Util.java
private static HttpURLConnection sendFormdata(String reqUrl, Bundle parameters, String fileParamName, String filename, String contentType, byte[] data) { HttpURLConnection urlConn = null; try {//ww w . j a va 2s. com URL url = new URL(reqUrl); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setConnectTimeout(5000);// ??jdk urlConn.setReadTimeout(5000);// ??jdk 1.5??,? urlConn.setDoOutput(true); urlConn.setRequestProperty("connection", "keep-alive"); String boundary = "-----------------------------114975832116442893661388290519"; // urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); boundary = "--" + boundary; StringBuffer params = new StringBuffer(); if (parameters != null) { for (Iterator<String> iter = parameters.keySet().iterator(); iter.hasNext();) { String name = iter.next(); String value = parameters.getString(name); params.append(boundary + "\r\n"); params.append("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n"); // params.append(URLEncoder.encode(value, "UTF-8")); params.append(value); params.append("\r\n"); } } StringBuilder sb = new StringBuilder(); sb.append(boundary); sb.append("\r\n"); sb.append("Content-Disposition: form-data; name=\"" + fileParamName + "\"; filename=\"" + filename + "\"\r\n"); sb.append("Content-Type: " + contentType + "\r\n\r\n"); byte[] fileDiv = sb.toString().getBytes(); byte[] endData = ("\r\n" + boundary + "--\r\n").getBytes(); byte[] ps = params.toString().getBytes(); OutputStream os = urlConn.getOutputStream(); os.write(ps); os.write(fileDiv); os.write(data); os.write(endData); os.flush(); os.close(); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } return urlConn; }
From source file:com.qpark.eip.core.spring.security.https.HttpsRequester.java
private static String read(final URL url, final String httpAuthBase64, final HostnameVerifier hostnameVerifier) throws IOException { StringBuffer sb = new StringBuffer(1024); HttpURLConnection connection = null; connection = (HttpURLConnection) url.openConnection(); if (url.getProtocol().equalsIgnoreCase("https")) { connection = (HttpsURLConnection) url.openConnection(); ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier); }/* w w w . j a v a2s . co m*/ if (httpAuthBase64 != null) { connection.setRequestProperty("Authorization", new StringBuffer(httpAuthBase64.length() + 6) .append("Basic ").append(httpAuthBase64).toString()); } connection.setRequestProperty("Content-Type", "text/plain; charset=\"utf8\""); connection.setRequestMethod("GET"); int returnCode = connection.getResponseCode(); InputStream connectionIn = null; if (returnCode == 200) { connectionIn = connection.getInputStream(); } else { connectionIn = connection.getErrorStream(); } BufferedReader buffer = new BufferedReader(new InputStreamReader(connectionIn)); String inputLine; while ((inputLine = buffer.readLine()) != null) { sb.append(inputLine); } buffer.close(); return sb.toString(); }
From source file:com.iStudy.Study.Renren.Util.java
private static HttpURLConnection openConn(String url, String method, Bundle params) { if (method.equals("GET")) { url = url + "?" + encodeUrl(params); }/*w w w . j a v a 2s .c om*/ try { Log.d(LOG_TAG, method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", USER_AGENT_SDK); if (!method.equals("GET")) { conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8")); } return conn; } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); throw new RuntimeException(e.getMessage(), e); } }
From source file:com.dynamobi.db.conn.couchdb.CouchUdx.java
/** * Creates a CouchDB view//from www . j av a 2s . com */ public static void makeView(String user, String pw, String url, String viewDef) throws SQLException { try { URL u = new URL(url); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); uc.setDoOutput(true); if (user != null && user.length() > 0) { uc.setRequestProperty("Authorization", "Basic " + buildAuthHeader(user, pw)); } uc.setRequestProperty("Content-Type", "application/json"); uc.setRequestMethod("PUT"); OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream()); wr.write(viewDef); wr.close(); String s = readStringFromConnection(uc); } catch (MalformedURLException e) { throw new SQLException("Bad URL."); } catch (IOException e) { throw new SQLException(e.getMessage()); } }
From source file:com.iStudy.Study.Renren.Util.java
/** * ??http/*from ww w . ja v a 2s .com*/ * * @param url * @param method GET POST * @param params * @return */ public static String openUrl(String url, String method, Bundle params) { if (method.equals("GET")) { url = url + "?" + encodeUrl(params); } String response = ""; try { Log.d(LOG_TAG, method + " URL: " + url); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", USER_AGENT_SDK); if (!method.equals("GET")) { conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.getOutputStream().write(encodeUrl(params).getBytes("UTF-8")); } InputStream is = null; int responseCode = conn.getResponseCode(); if (responseCode == 200) { is = conn.getInputStream(); } else { is = conn.getErrorStream(); } response = read(is); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } return response; }
From source file:net.ftb.util.DownloadUtils.java
/** * @param file - file on the repo in static * @return boolean representing if the file exists */// ww w . j a v a 2 s. c om public static boolean staticFileExists(String file) { try { HttpURLConnection connection = (HttpURLConnection) new URL(getStaticCreeperhostLink(file)) .openConnection(); connection.setRequestProperty("Cache-Control", "no-transform"); connection.setRequestMethod("HEAD"); return (connection.getResponseCode() == 200); } catch (Exception e) { return false; } }
From source file:net.ftb.util.DownloadUtils.java
/** * @param file - file on the repo//from w w w. j a v a 2 s . c om * @return boolean representing if the file exists */ public static boolean fileExists(String file) { try { HttpURLConnection connection = (HttpURLConnection) new URL(Locations.masterRepo + "/FTB2/" + file) .openConnection(); connection.setRequestProperty("Cache-Control", "no-transform"); connection.setRequestMethod("HEAD"); return (connection.getResponseCode() == 200); } catch (Exception e) { return false; } }
From source file:io.andyc.papercut.api.PrintApi.java
/** * Queries the remote Papercut server to get the status of the file * * e.g./*from ww w.j av a 2s . com*/ * * {"status":{"code":"hold-release","complete":false,"text":"Held in a * queue","formatted":"Held in a queue"},"documentName":"test.rtf", * "printer":"Floor1 (Full Colour)","pages":1,"cost":"$0.10"} * * @param printJob {PrintJob} - the file to check the status on * * @return {String} - JSON String which includes file metadata * * @throws NoStatusURLSetException * @throws IOException */ public static PrintStatusData getFileStatus(PrintJob printJob) throws NoStatusURLSetException, IOException { if (Objects.equals(printJob.getStatusCheckURL(), "") || printJob.getStatusCheckURL() == null) { throw new NoStatusURLSetException(); } // from: http://stackoverflow.com/a/29889139/2605221 StringBuilder stringBuffer = new StringBuilder(""); URL url = new URL(printJob.getStatusCheckURL()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", ""); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/json"); connection.setDoInput(true); connection.connect(); InputStream inputStream = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; while ((line = rd.readLine()) != null) { stringBuffer.append(line); } return new ObjectMapper().readValue(stringBuffer.toString(), PrintStatusData.class); }