本文整理了Java中org.apache.http.HttpResponse
类的一些代码示例,展示了HttpResponse
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse
类的具体详情如下:
包路径:org.apache.http.HttpResponse
类名称:HttpResponse
[英]An HTTP response.
[中]
代码示例来源: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: 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: rest-assured/rest-assured
public Header[] getAllHeaders() {
return responseBase.getAllHeaders();
}
代码示例来源:origin: aws/aws-sdk-java
private static boolean isTemporaryRedirect(org.apache.http.HttpResponse response) {
int status = response.getStatusLine().getStatusCode();
return status == HttpStatus.SC_TEMPORARY_REDIRECT && response.getHeaders("Location") != null
&& response.getHeaders("Location").length > 0;
}
代码示例来源:origin: ctripcorp/apollo
private void checkHttpResponseStatus(HttpResponse response) {
if (response.getStatusLine().getStatusCode() == 200) {
return;
}
StatusLine status = response.getStatusLine();
String message = "";
try {
message = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
//ignore
}
throw new ApolloOpenApiException(status.getStatusCode(), status.getReasonPhrase(), message);
}
代码示例来源: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
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
代码示例来源: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: robolectric/robolectric
@Test
public void realHttpRequestsShouldMakeContentDataAvailable() throws Exception {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
FakeHttp.getFakeHttpLayer().interceptResponseContent(true);
DefaultHttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));
byte[] cachedContent = FakeHttp.getFakeHttpLayer().getHttpResposeContentList().get(0);
assertThat(cachedContent.length).isNotEqualTo(0);
InputStream content =
FakeHttp.getFakeHttpLayer().getLastHttpResponse().getEntity().getContent();
BufferedReader contentReader = new BufferedReader(new InputStreamReader(content, UTF_8));
String firstLineOfContent = contentReader.readLine();
assertThat(firstLineOfContent).contains("Google");
BufferedReader cacheReader =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(cachedContent), UTF_8));
String firstLineOfCachedContent = cacheReader.readLine();
assertThat(firstLineOfCachedContent).isEqualTo(firstLineOfContent);
}
代码示例来源: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"));
}
});
代码示例来源:origin: robolectric/robolectric
@Test
public void testExecute() throws IOException {
AndroidHttpClient client = AndroidHttpClient.newInstance("foo");
FakeHttp.addPendingHttpResponse(200, "foo");
HttpResponse resp = client.execute(new HttpGet("/foo"));
assertThat(resp.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(CharStreams.toString(new InputStreamReader(resp.getEntity().getContent(), UTF_8)))
.isEqualTo("foo");
}
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldForwardHttpsAndHostOnDifferentPort() throws Exception
{
URI rootUri = functionalTestHelper.baseUri();
HttpClient httpclient = new DefaultHttpClient();
try
{
HttpGet httpget = new HttpGet( rootUri );
httpget.setHeader( "Accept", "application/json" );
httpget.setHeader( "X-Forwarded-Host", "foobar.com:9999" );
httpget.setHeader( "X-Forwarded-Proto", "https" );
HttpResponse response = httpclient.execute( httpget );
String length = response.getHeaders( "CONTENT-LENGTH" )[0].getValue();
byte[] data = new byte[Integer.valueOf( length )];
response.getEntity().getContent().read( data );
String responseEntityBody = new String( data );
assertTrue( responseEntityBody.contains( "https://foobar.com:9999" ) );
assertFalse( responseEntityBody.contains( "https://localhost" ) );
}
finally
{
httpclient.getConnectionManager().shutdown();
}
}
代码示例来源: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: apache/nifi
private JSONObject readJSONObjectFromUrlPOST(String urlString, Map<String, String> headers, String payload) throws IOException, JSONException {
HttpClient httpClient = openConnection();
HttpPost request = new HttpPost(urlString);
for (Map.Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
HttpEntity httpEntity = new StringEntity(payload);
request.setEntity(httpEntity);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " : " + response.getStatusLine().getReasonPhrase());
}
InputStream content = response.getEntity().getContent();
return readAllIntoJSONObject(content);
}
代码示例来源:origin: shuzheng/zheng
HttpPost httpPost = null;
try {
HttpClient httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String result = EntityUtils.toString(resEntity, Charset.forName("utf-8"));
代码示例来源:origin: stackoverflow.com
HttpHost proxy = new HttpHost("ip address",port number);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param name", param));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
InputStream in = entity.getContent();
httpclient.getConnectionManager().shutdown();
代码示例来源: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: apache/hbase
/**
* Send a GET request
* @param c the cluster definition
* @param path the path or URI
* @param headers the HTTP headers to include in the request
* @return a Response object with response detail
* @throws IOException
*/
public Response get(Cluster c, String path, Header[] headers)
throws IOException {
if (httpGet != null) {
httpGet.releaseConnection();
}
httpGet = new HttpGet(path);
HttpResponse resp = execute(c, httpGet, headers, path);
return new Response(resp.getStatusLine().getStatusCode(), resp.getAllHeaders(),
resp, resp.getEntity() == null ? null : resp.getEntity().getContent());
}
代码示例来源:origin: apache/incubator-dubbo
@Override
public InputStream getInputStream() throws IOException {
return response == null || response.getEntity() == null ? null : response.getEntity().getContent();
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldParameteriseUrisInNodeRepresentationWithoutHostHeaderUsingRequestUri() throws Exception
{
try ( CloseableHttpClient httpclient = HttpClientBuilder.create().build() )
{
HttpGet httpget = new HttpGet( nodeUri );
httpget.setHeader( "Accept", "application/json" );
HttpResponse response = httpclient.execute( httpget );
HttpEntity entity = response.getEntity();
String entityBody = IOUtils.toString( entity.getContent(), StandardCharsets.UTF_8 );
assertThat( entityBody, containsString( nodeUri.toString() ) );
}
}
内容来源于网络,如有侵权,请联系作者删除!