@Test public void testPost_shouldUseCorrectMethod() throws Exception { server.enqueue(new MockResponse().setBody("it's all cool")); server.play(); StringEntity stringEntity = new StringEntity("a post body"); http.post(server.getUrl("/").toString(), new HashMap<String, String>(), stringEntity, false); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getMethod(), equalTo(HttpPost.METHOD_NAME)); assertThat(recordedRequest.getUtf8Body(), equalTo("a post body")); }
public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); }
Http { public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); } }
Http { public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); }  }
Http { public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); }  static void initCookieManager(Context context); static void clearCookie(Context context); Response get(String url, Map<String, String> headers, boolean allowRedirect); Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect); static byte[] unGZIP(byte[] zipped); static ByteArrayInputStream readDataForZgip(InputStream inStream, String charsetName); static ByteArrayInputStream read(InputStream inStream, String charsetName); }
Http { public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); }  static void initCookieManager(Context context); static void clearCookie(Context context); Response get(String url, Map<String, String> headers, boolean allowRedirect); Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect); static byte[] unGZIP(byte[] zipped); static ByteArrayInputStream readDataForZgip(InputStream inStream, String charsetName); static ByteArrayInputStream read(InputStream inStream, String charsetName);  }