org.apache.http.HttpResponse.getEntity()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(813)

本文整理了Java中org.apache.http.HttpResponse.getEntity()方法的一些代码示例,展示了HttpResponse.getEntity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.getEntity()方法的具体详情如下:
包路径:org.apache.http.HttpResponse
类名称:HttpResponse
方法名:getEntity

HttpResponse.getEntity介绍

[英]Obtains the message entity of this response, if any. The entity is provided by calling #setEntity.
[中]获取此响应的消息实体(如果有)。实体是通过调用#setEntity提供的。

代码示例

代码示例来源:origin: apache/incubator-dubbo

@Override
public InputStream getInputStream() throws IOException {
  return response == null || response.getEntity() == null ? null : response.getEntity().getContent();
}

代码示例来源:origin: stackoverflow.com

HttpClient httpclient = new DefaultHttpClient();
 HttpResponse response = httpclient.execute(new HttpGet(URL));
 StatusLine statusLine = response.getStatusLine();
 if(statusLine.getStatusCode() == HttpStatus.SC_OK){
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   response.getEntity().writeTo(out);
   String responseString = out.toString();
   out.close();
   //..more logic
 } else{
   //Closes the connection.
   response.getEntity().getContent().close();
   throw new IOException(statusLine.getReasonPhrase());
 }

代码示例来源:origin: stackoverflow.com

public void post() throws Exception{
   HttpClient client = new DefaultHttpClient();
   HttpPost post = new HttpPost("http://www.baidu.com");
   String xml = "<xml>xxxx</xml>";
   HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
   post.setEntity(entity);
   HttpResponse response = client.execute(post);
   String result = EntityUtils.toString(response.getEntity());
 }

代码示例来源:origin: stackoverflow.com

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
  sb.append(line + "\n");

String resString = sb.toString(); // Result is here

is.close(); // Close the stream

代码示例来源:origin: pentaho/pentaho-kettle

protected InputStreamReader openStream( String encoding, HttpResponse httpResponse ) throws Exception {
 if ( !Utils.isEmpty( encoding ) ) {
  return new InputStreamReader( httpResponse.getEntity().getContent(), encoding );
 } else {
  return new InputStreamReader( httpResponse.getEntity().getContent() );
 }
}

代码示例来源:origin: stackoverflow.com

URL url=new URL(urlToHit);

HttpPost httppost = new HttpPost(url.toString());
HttpResponse response = LoginScreen.httpClient.execute(httppost); 

// Log.v("response code",""+response.getStatusLine().getStatusCode());

// Get hold of the response entity
HttpEntity entity = response.getEntity();

InputStream instream = null;

if (entity != null) {
  instream = entity.getContent();
}
xr.parse(new InputSource(instream)); //SAX parsing

代码示例来源:origin: stackoverflow.com

HttpEntity entity = MultipartEntityBuilder
  .create()
  .addTextBody("number", "5555555555")
  .addTextBody("clip", "rickroll")
  .addBinaryBody("upload_file", new File(filePath), ContentType.create("application/octet-stream"), "filename")
  .addTextBody("tos", "agree")
  .build();

HttpPost httpPost = new HttpPost("http://some-web-site");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity result = response.getEntity();

代码示例来源:origin: apache/nifi

private JSONObject readJSONFromUrl(String urlString, Map<String, String> headers) throws IOException, JSONException {
  HttpClient httpClient = openConnection();
  HttpGet request = new HttpGet(urlString);
  for (Map.Entry<String, String> entry : headers.entrySet()) {
    request.addHeader(entry.getKey(), entry.getValue());
  }
  HttpResponse response = httpClient.execute(request);
  InputStream content = response.getEntity().getContent();
  return readAllIntoJSONObject(content);
}

代码示例来源:origin: dreamhead/moco

private Plain asPlain(org.apache.http.HttpResponse response) throws IOException {
  assertThat(response.getStatusLine().getStatusCode(), is(200));
  HttpEntity entity = response.getEntity();
  MediaType mediaType = MediaType.parse(entity.getContentType().getValue());
  assertThat(mediaType.type(), is("application"));
  assertThat(mediaType.subtype(), is("json"));
  return Jsons.toObject(entity.getContent(), Plain.class);
}

代码示例来源:origin: stackoverflow.com

String encoding = Base64Encoder.encode ("test1:test1");
HttpPost httppost = new HttpPost("http://host:post/test/login");
httppost.setHeader("Authorization", "Basic " + encoding);

System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

代码示例来源:origin: nostra13/Android-Universal-Image-Loader

@Override
  protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
    HttpGet httpRequest = new HttpGet(imageUri);
    HttpResponse response = httpClient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    return bufHttpEntity.getContent();
  }
}

代码示例来源:origin: stackoverflow.com

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
  str.append(line);
}
in.close();
html = str.toString();

代码示例来源:origin: stackoverflow.com

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
  InputStream instream = entity.getContent();
  try {
    // do something useful
  } finally {
    instream.close();
  }
}

代码示例来源:origin: robolectric/robolectric

private static String getStringContent(HttpResponse response) throws IOException {
 return CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), UTF_8));
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public InputStream getInputStream() throws IOException {
  return response == null || response.getEntity() == null ? null : response.getEntity().getContent();
}

代码示例来源:origin: stackoverflow.com

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
  response = httpclient.execute(new HttpGet(uri[0]));
  StatusLine statusLine = response.getStatusLine();
  if(statusLine.getStatusCode() == HttpStatus.SC_OK){
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    responseString = out.toString();
    out.close();
  } else{
    response.getEntity().getContent().close();
    throw new IOException(statusLine.getReasonPhrase());

代码示例来源:origin: dreamhead/moco

private void assertJson(final String url, final String content) throws IOException {
    HttpResponse response = helper.getResponse(url);
    HttpEntity entity = response.getEntity();
    byte[] bytes = ByteStreams.toByteArray(entity.getContent());
    assertThat(new String(bytes), is(content));
    MediaType mediaType = MediaType.parse(entity.getContentType().getValue());
    assertThat(mediaType.type(), is("application"));
    assertThat(mediaType.subtype(), is("json"));
  }
}

代码示例来源:origin: stackoverflow.com

DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
  HttpResponse response = httpclient.execute(httppost);           
  HttpEntity entity = response.getEntity();

  inputStream = entity.getContent();
  // json is UTF-8 by default
  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
  StringBuilder sb = new StringBuilder();

  String line = null;
  while ((line = reader.readLine()) != null)
  {
    sb.append(line + "\n");
  }
  result = sb.toString();
} catch (Exception e) { 
  // Oops
}
finally {
  try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}

代码示例来源:origin: stackoverflow.com

HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();

String responseText = EntityUtils.toString(entity);

代码示例来源:origin: dreamhead/moco

@Override
  public void run() throws IOException {
    org.apache.http.HttpResponse httpResponse = helper.getResponse(remoteUrl("/dir/dir.response"));
    String value = httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
    assertThat(value, is("text/plain"));
    String content = CharStreams.toString(new InputStreamReader(httpResponse.getEntity().getContent()));
    assertThat(content, is("response from dir"));
  }
});

相关文章