public HttpKnife option(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.OPTIONS); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
public HttpKnife put(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.PUT); connection.setDoOutput(true); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } return this; }
public HttpKnife head(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.HEAD); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
public HttpKnife trace(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.TRACE); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
public HttpKnife patch(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.PATCH); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
public HttpKnife delete(String url) { try { System.out.println("delete request : " + url); openConnection(new URL(url)); connection.setRequestMethod(Method.DELETE); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
private static HttpURLConnection doHttp(String url, int timeout, String method, boolean doinput, boolean dooutput) { HttpURLConnection con = null; try { URL url1 = new URL(url); con = (HttpURLConnection) url1.openConnection(); con.setReadTimeout(timeout); //将读超时设置为指定的超时,以毫秒为单位。用一个非零值指定在建立到资源的连接后从 Input 流读入时的超时时间。如果在数据可读取之前超时期满,则会引发一个 java.net.SocketTimeoutException。 con.setDoInput(doinput); //指示应用程序要从 URL 连接读取数据。 con.setDoOutput(dooutput); con.setRequestMethod(method); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return con; }
/** * 不带参数的get请求 * * @param url * @return */ public HttpKnife get(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.GET); return this; } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } return null; }
@Override protected Void doInBackground(Void... params) { try { //TODO Replace below url with your server side file url. URL url = new URL("https://payu.herokuapp.com/delete_merchant_hash"); byte[] postParamsByte = postParams.getBytes("UTF-8"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length)); conn.setDoOutput(true); conn.getOutputStream().write(postParamsByte); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
/** * post请求初始化 * * @param url * @throws ProtocolException * @throws MalformedURLException */ public HttpKnife post(String url) { try { openConnection(new URL(url)); connection.setRequestMethod(Method.POST); connection.setDoOutput(true); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } return this; }
public static String doGet(String url) { try { HttpURLConnection con = doHttp(url, 500000, "GET", true, false); if (con.getResponseCode() == 200) //当请求成功时,接收数据(状态码“200”为成功连接的意思“ok”) { InputStream is = con.getInputStream(); str = formatIsToString(is); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return str; }
/** * 获取网站的favicon图标大小 * * @param faviocnUrl * favicon地址 * @return favicon图标大小 */ private static int getFaviconSize(String faviocnUrl) { int contentLength = 0; try { final URL url = new URL(faviocnUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setDoOutput(true); // 必须设置false,否则会自动redirect到Location的地址 connection.setInstanceFollowRedirects(false); contentLength = connection.getContentLength(); log.debug("Favicon size : {}", contentLength); } catch (MalformedURLException e) { e.printStackTrace(); log.error("请求地址不对", e); } catch (ProtocolException e) { e.printStackTrace(); log.error("请求地址协议异常", e); } catch (IOException e) { e.printStackTrace(); log.error("数据传输异常", e); } return contentLength; }
e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace();
} catch (ProtocolException e) e.printStackTrace();
return null; } catch (ProtocolException e) { e.printStackTrace(); return null; } catch (IOException e) {
public static String doPost(String url, Map<String, String> params) { try { HttpURLConnection con = doHttp(url, 500000, "POST", true, true); //指示应用程序要将数据写入 URL 连接。 String content = getContent(params); //解析参数(请求的内容) con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置内容 con.setRequestProperty("Content-Length", content.length() + ""); //设置内容长度 OutputStream os = con.getOutputStream(); os.write(content.getBytes("utf-8")); //发送参数内容 os.flush(); os.close(); if (con.getResponseCode() == 200) { str = formatIsToString(con.getInputStream()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return str; }
e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace();
public HttpKnife json(JSONObject json) { try { String contentType = getBodyContentType(CONTENT_TYPE_JSON, getParamsEncoding()); header(RequestHeader.CONTENT_TYPE, contentType); byte[] body = json.toString().getBytes(getParamsEncoding()); DataOutputStream out = new DataOutputStream(openOutput()); out.write(body); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return this; }
System.out.println("Service returned response code " + responseCode); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
System.out.println("Service returned response code " + responseCode); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();