/** * Generates response body for UNAUTHORIZED response. * * @param unauthorized the response to return as a result of authentication failure. * @return the response content entity. */ protected HttpEntity generateResponseContent(final HttpResponse unauthorized) { return new StringEntity("Unauthorized"); }
@Test public void testBasics() throws Exception { final String s = "Message content"; final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN); final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1); Assert.assertEquals(bytes.length, httpentity.getContentLength()); Assert.assertNotNull(httpentity.getContent()); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); }
@Test public void testDefaultContent() throws Exception { final String s = "Message content"; StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968")); Assert.assertEquals("text/csv; charset=US-ASCII", httpentity.getContentType()); httpentity = new StringEntity(s, StandardCharsets.US_ASCII); Assert.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType()); httpentity = new StringEntity(s); Assert.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType()); }
HttpPost sentHttpRequest = (HttpPost) Robolectric.getSentHttpRequest(0); StringEntity entity = (StringEntity) sentHttpRequest.getEntity(); String sentPostBody = fromStream(entity.getContent()); assertThat(sentPostBody, equalTo("a post body")); assertThat(entity.getContentType().getValue(), equalTo("text/plain; charset=UTF-8"));
@Test public void testBasics() throws Exception { final String s = "Message content"; final StringEntity httpentity = new StringEntity(s, ContentType.TEXT_PLAIN); httpentity.setContentEncoding("blah"); final HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity); Assert.assertEquals(httpentity.getContentLength(), wrapped.getContentLength()); Assert.assertEquals(httpentity.getContentType(), wrapped.getContentType()); Assert.assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding()); Assert.assertEquals(httpentity.isChunked(), wrapped.isChunked()); Assert.assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable()); Assert.assertEquals(httpentity.isStreaming(), wrapped.isStreaming()); Assert.assertNotNull(wrapped.getContent()); }
@Test public void testTerminateRequestContentLengthShort() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Content-Length", "3"); final StringEntity entity = new StringEntity("123", ContentType.TEXT_PLAIN); entity.setChunked(false); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: " + "3\r\n\r\n123", s); Assert.assertTrue(conn.isConsistent()); }
@Test public void testParseEntity() throws Exception { try (final StringEntity entity = new StringEntity("Name1=Value1")) { entity.setContentType(URLEncodedUtils.CONTENT_TYPE); final List<NameValuePair> result = EntityUtils.parse(entity); Assert.assertEquals(1, result.size()); assertNameValuePair(result.get(0), "Name1", "Value1"); entity.setContentType("text/test"); Assert.assertTrue(EntityUtils.parse(entity).isEmpty()); }}
@Test public void testWriteTo() throws Exception { final String s = "Message content"; final byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1); final StringEntity httpentity = new StringEntity(s); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } out = new ByteArrayOutputStream(); httpentity.writeTo(out); bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
/** * Creates a StringEntity with the specified content and content type. * * @param string content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case the default * MIME type {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * this instance of the Java virtual machine * @since 4.2 */ public StringEntity(final String string, final ContentType contentType) { super(); Args.notNull(string, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = StandardCharsets.ISO_8859_1; } this.content = string.getBytes(charset); if (contentType != null) { setContentType(contentType.toString()); } }
HttpParams httpParams=new BasicHttpParams(); HttpClient httpclient = new DefaultHttpClient(httpParams); HttpPost httppost = new HttpPost(myURL); // I can't show my url but it's a verified .asp working both with iOS and browser try { // Add your data String str = "param1=test¶m2=test2¶m3=test3"; StringEntity strEntity = new StringEntity(str, HTTP.UTF_8); httppost.setEntity(strEntity); httppost.setHeader("Set-Cookie", sessionCookie); httppost.setHeader("Accept", "text/html"); httppost.setHeader("Content-Type", "application/x-www-form-urlencoded"); httppost.setHeader("Content-Length", strEntity.getContentLength()+""); // Execute HTTP Post Request BasicResponseHandler brh = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, brh); System.out.println(responseBody); } catch (ClientProtocolException e) { // I keep catching this exception e.printStackTrace(); } catch (IOException e) { System.err.println("Check network"); }
@Test public void testRequestContentEntityChunkedHTTP10() throws Exception { final HttpContext context = new BasicHttpContext(null); context.setProtocolVersion(HttpVersion.HTTP_1_0); final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); entity.setChunked(true); request.setEntity(entity); final RequestContent interceptor = new RequestContent(); try { interceptor.process(request, request.getEntity(), context); Assert.fail("ProtocolException should have been thrown"); } catch (final ProtocolException ex) { // expected } }
/** * Creates a StringEntity with the specified content and content type. * * @param string content to be used. Not {@code null}. * @param contentType content type to be used. May be {@code null}, in which case the default * MIME type {@link ContentType#TEXT_PLAIN} is assumed. * * @throws IllegalArgumentException if the string parameter is null * this instance of the Java virtual machine * @since 4.2 */ public StringEntity(final String string, final ContentType contentType) { super(); Args.notNull(string, "Source string"); Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = StandardCharsets.ISO_8859_1; } this.content = string.getBytes(charset); if (contentType != null) { setContentType(contentType.toString()); } }
post.setHeader("Content-length",""+se.getContentLength()); //header has to be set after jsonparams are complete post.setEntity(se);
/** * Generates response body for UNAUTHORIZED response. * * @param unauthorized the response to return as a result of authentication failure. * @return the response content entity. */ protected HttpEntity generateResponseContent(final HttpResponse unauthorized) { return new StringEntity("Unauthorized"); }
@Test public void testTerminateRequestChunkedEntity() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Transfer-Encoding", "chunked"); final StringEntity entity = new StringEntity("123", ContentType.TEXT_PLAIN); entity.setChunked(true); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nTransfer-Encoding: " + "chunked\r\n\r\n0\r\n\r\n", s); Assert.assertTrue(conn.isConsistent()); }
@Test public void testNullCharset() throws Exception { final String s = constructString(SWISS_GERMAN_HELLO); StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null)); Assert.assertNotNull(httpentity.getContentType()); Assert.assertEquals("text/plain", httpentity.getContentType()); Assert.assertEquals(s, EntityUtils.toString(httpentity)); httpentity = new StringEntity(s, (Charset) null); Assert.assertNotNull(httpentity.getContentType()); Assert.assertEquals("text/plain", httpentity.getContentType()); Assert.assertEquals(s, EntityUtils.toString(httpentity)); }
private static ClassicHttpResponse convert(final SimpleHttpResponse cacheResponse, final ExecChain.Scope scope) { if (cacheResponse == null) { return null; } final ClassicHttpResponse response = new BasicClassicHttpResponse(cacheResponse.getCode(), cacheResponse.getReasonPhrase()); for (final Iterator<Header> it = cacheResponse.headerIterator(); it.hasNext(); ) { response.addHeader(it.next()); } response.setVersion(cacheResponse.getVersion() != null ? cacheResponse.getVersion() : HttpVersion.DEFAULT); final SimpleBody body = cacheResponse.getBody(); if (body != null) { if (body.isText()) { response.setEntity(new StringEntity(body.getBodyText(), body.getContentType())); } else { response.setEntity(new ByteArrayEntity(body.getBodyBytes(), body.getContentType())); } } scope.clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); return response; }
@Test public void testRequestContentEntityChunkedHTTP11() throws Exception { final HttpContext context = new BasicHttpContext(null); final BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/"); final String s = "whatever"; final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII); entity.setChunked(true); request.setEntity(entity); final RequestContent interceptor = new RequestContent(); interceptor.process(request, request.getEntity(), context); final Header header = request.getFirstHeader(HttpHeaders.TRANSFER_ENCODING); Assert.assertNotNull(header); Assert.assertEquals("chunked", header.getValue()); Assert.assertNull(request.getFirstHeader(HttpHeaders.CONTENT_LENGTH)); }
StringEntity stringEntity = new StringEntity(stringWriter.toString(), ContentType.APPLICATION_JSON); httpPut.setEntity(stringEntity); addHeaders(httpPut, EntityUtils.toString(stringEntity), ContentType.APPLICATION_JSON.toString());
customizeUnauthorizedResponse(unauthorized); if (unauthorized.getEntity() == null) { unauthorized.setEntity(new StringEntity("Unauthorized"));