Package org.apache.http.entity

Examples of org.apache.http.entity.StringEntity


  public void smallHttpPostBodyWithUnusualCharactersTest() throws ClientProtocolException, IOException {
    final String body = "Räger Schildmäijår";
   
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/echo");
    httppost.setEntity(new StringEntity(body))// HTTP 1.1 says that the default charset is ISO-8859-1
    HttpResponse response = httpclient.execute(httppost)
   
    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here


        try
        {
            URI resourceUri = getResourceURI(resource.getId());
            HttpPut putMethod = new HttpPut(resourceUri);
            putMethod.setHeader("Connection", "close");
            StringEntity stringEntity = new StringEntity(resource.getContent());
            stringEntity.setContentType(resource.getContentType());
            stringEntity.setContentEncoding("UTF-8");
            putMethod.setEntity(stringEntity);
            Credentials credentials =
                    new UsernamePasswordCredentials(getUserName(), password);
            httpClient.getCredentialsProvider().
                    setCredentials(AuthScope.ANY, credentials);
View Full Code Here

    try {
      // prepare POST body
      String body = methodCall(method, params);

      // set POST body
      HttpEntity entity = new StringEntity(body);
      postMethod.setEntity(entity);

      // This code slightly tweaked from the code by erickok in issue #6
      // Force preemptive authentication
      // This makes sure there is an 'Authentication: ' header being send before trying and failing and retrying
      // by the basic authentication mechanism of DefaultHttpClient
      if(this.httpPreAuth == true) {
        String auth = this.username + ":" + this.password;
        postMethod.addHeader("Authorization", "Basic " + Base64Coder.encode(auth.getBytes()).toString());
      }
     
      //Log.d(Tag.LOG, "ros HTTP POST");
      // execute HTTP POST request
      HttpResponse response = client.execute(postMethod);
      //Log.d(Tag.LOG, "ros HTTP POSTed");

      // check status code
      int statusCode = response.getStatusLine().getStatusCode();
      //Log.d(Tag.LOG, "ros status code:" + statusCode);
      if (statusCode != HttpStatus.SC_OK) {
        throw new XMLRPCException("HTTP status code: " + statusCode + " != " + HttpStatus.SC_OK);
      }

      // parse response stuff
      //
      // setup pull parser
      XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
      entity = response.getEntity();
      Reader reader = new InputStreamReader(new BufferedInputStream(entity.getContent()));
// for testing purposes only
// reader = new StringReader("<?xml version='1.0'?><methodResponse><params><param><value>\n\n\n</value></param></params></methodResponse>");
      pullParser.setInput(reader);
     
      // lets start pulling...
      pullParser.nextTag();
      pullParser.require(XmlPullParser.START_TAG, null, Tag.METHOD_RESPONSE);
     
      pullParser.nextTag(); // either Tag.PARAMS (<params>) or Tag.FAULT (<fault>) 
      String tag = pullParser.getName();
      if (tag.equals(Tag.PARAMS)) {
        // normal response
        pullParser.nextTag(); // Tag.PARAM (<param>)
        pullParser.require(XmlPullParser.START_TAG, null, Tag.PARAM);
        pullParser.nextTag(); // Tag.VALUE (<value>)
        // no parser.require() here since its called in XMLRPCSerializer.deserialize() below
       
        // deserialize result
        Object obj = iXMLRPCSerializer.deserialize(pullParser);
        entity.consumeContent();
        return obj;
      } else
      if (tag.equals(Tag.FAULT)) {
        // fault response
        pullParser.nextTag(); // Tag.VALUE (<value>)
        // no parser.require() here since its called in XMLRPCSerializer.deserialize() below

        // deserialize fault result
        Map<String, Object> map = (Map<String, Object>) iXMLRPCSerializer.deserialize(pullParser);
        String faultString = (String) map.get(Tag.FAULT_STRING);
        int faultCode = (Integer) map.get(Tag.FAULT_CODE);
        entity.consumeContent();
        throw new XMLRPCFault(faultString, faultCode);
      } else {
        entity.consumeContent();
        throw new XMLRPCException("Bad tag <" + tag + "> in XMLRPC response - neither <params> nor <fault>");
      }
    } catch (XMLRPCException e) {
      // catch & propagate XMLRPCException/XMLRPCFault
      throw e;
View Full Code Here

  }

  @Test
  public void testSimple() throws IOException, InterruptedException {

    StringEntity input = new StringEntity("[{\"headers\":{\"a\": \"b\"},\"body\": \"random_body\"},"
            + "{\"headers\":{\"e\": \"f\"},\"body\": \"random_body2\"}]");
    //if we do not set the content type to JSON, the client will use
    //ISO-8859-1 as the charset. JSON standard does not support this.
    input.setContentType("application/json");
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    Assert.assertEquals(HttpServletResponse.SC_OK,
View Full Code Here

  }

  @Test
  public void testSimpleUTF16() throws IOException, InterruptedException {

    StringEntity input = new StringEntity("[{\"headers\":{\"a\": \"b\"},\"body\": \"random_body\"},"
            + "{\"headers\":{\"e\": \"f\"},\"body\": \"random_body2\"}]", "UTF-16");
    input.setContentType("application/json; charset=utf-16");
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    Assert.assertEquals(HttpServletResponse.SC_OK,
View Full Code Here

    tx.close();
  }

  @Test
  public void testInvalid() throws Exception {
    StringEntity input = new StringEntity("[{\"a\": \"b\",[\"d\":\"e\"],\"body\": \"random_body\"},"
            + "{\"e\": \"f\",\"body\": \"random_body2\"}]");
    input.setContentType("application/json");
    postRequest.setEntity(input);
    HttpResponse response = httpClient.execute(postRequest);

    Assert.assertEquals(HttpServletResponse.SC_BAD_REQUEST,
            response.getStatusLine().getStatusCode());
View Full Code Here

  public void testBigBatchDeserializarionUTF32() throws Exception {
    testBatchWithVariousEncoding("UTF-32");
  }
  @Test
  public void testSingleEvent() throws Exception {
    StringEntity input = new StringEntity("[{\"headers\" : {\"a\": \"b\"},\"body\":"
            + " \"random_body\"}]");
    input.setContentType("application/json");
    postRequest.setEntity(input);

    httpClient.execute(postRequest);
    Transaction tx = channel.getTransaction();
    tx.begin();
View Full Code Here

      e.setBody(String.valueOf(rand.nextGaussian()).getBytes(encoding));
      events.add(e);
    }
    Gson gson = new Gson();
    String json = gson.toJson(events, listType);
    StringEntity input = new StringEntity(json);
    input.setContentType("application/json; charset=" + encoding);
    postRequest.setEntity(input);
    HttpResponse resp = httpClient.execute(postRequest);
    return new ResultWrapper(resp, events);
  }
View Full Code Here

            if (uri.equals("/oldlocation/")) {
                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
                response.addHeader(new BasicHeader("Location", "/relativelocation/"));
            } else if (uri.equals("/relativelocation/")) {
                response.setStatusLine(ver, HttpStatus.SC_OK);
                StringEntity entity = new StringEntity("Successful redirect");
                response.setEntity(entity);
            } else {
                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
            }
        }
View Full Code Here

            if (uri.equals("/test/oldlocation")) {
                response.setStatusLine(ver, HttpStatus.SC_MOVED_TEMPORARILY);
                response.addHeader(new BasicHeader("Location", "relativelocation"));
            } else if (uri.equals("/test/relativelocation")) {
                response.setStatusLine(ver, HttpStatus.SC_OK);
                StringEntity entity = new StringEntity("Successful redirect");
                response.setEntity(entity);
            } else {
                response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.http.entity.StringEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.