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

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

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

HttpResponse.getStatusLine介绍

[英]Obtains the status line of this response. The status line can be set using one of the #setStatusLine methods, or it can be initialized in a constructor.
[中]获取此响应的状态行。可以使用#setStatusLine方法之一设置状态行,也可以在构造函数中初始化状态行。

代码示例

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

@Override
public int getStatusCode() {
  return response == null || response.getStatusLine() == null ? 0 : response.getStatusLine().getStatusCode();
}

代码示例来源: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: 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: apache/kylin

public String getLookupSnapshotCacheState(String lookupTableName, String snapshotID) throws IOException {
  String url = baseUrl + "/tables/" + lookupTableName + "/" + snapshotID + "/snapshotLocalCache/state";
  HttpGet get = new HttpGet(url);
  HttpResponse response = client.execute(get);
  String content = getContent(response);
  if (response.getStatusLine().getStatusCode() != 200) {
    throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + WITH_URL + url + "\n");
  }
  return content;
}

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

httpclient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpget = new HttpGet("https://10.2.20.20/fido/EzPay/login.php");
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
  System.out.println("Response content length:  " + entity.getContentLength());
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) {

代码示例来源:origin: opentripplanner/OpenTripPlanner

public static void testUrl(String url) throws IOException {
  HttpHead head = new HttpHead(url);
  HttpClient httpclient = getClient();
  HttpResponse response = httpclient.execute(head);
  StatusLine status = response.getStatusLine();
  if (status.getStatusCode() == 404) {
    throw new FileNotFoundException();
  }
  if (status.getStatusCode() != 200) {
    throw new RuntimeException("Could not get URL: " + status.getStatusCode() + ": "
        + status.getReasonPhrase());
  }
}

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

HttpClient httpClient = HttpClientBuilder.create().build();
  HttpGet request = new HttpGet(crawlUrl);
  HttpResponse rsp = httpClient.execute(request);
  int statusCode = rsp.getStatusLine().getStatusCode();
  if (statusCode == 200) {
     String content = EntityUtils.toString(rsp.getEntity());    
     Document doc = Jsoup.parse(content);
     //parse content, whatever you need
     Element price = doc.select("[itemprop=price]").first();
  }

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

private void sendData(ArrayList<NameValuePair> data)
{
   // 1) Connect via HTTP. 2) Encode data. 3) Send data.
  try
  {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new      
    HttpPost("http://www.blah.com/AddAccelerationData.php");
    httppost.setEntity(new UrlEncodedFormEntity(data));
    HttpResponse response = httpclient.execute(httppost);
    Log.i("postData", response.getStatusLine().toString());
      //Could do something better with response.
  }
  catch(Exception e)
  {
    Log.e("log_tag", "Error:  "+e.toString());
  }  
}

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

public static HttpResponse mockHttpResponse(int statusCode, String message, String body) {
  HttpResponse response = Mockito.mock(HttpResponse.class);
  Mockito.when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, message));
  Mockito.when(response.getEntity()).thenReturn(new StringEntity(body, StandardCharsets.UTF_8));
  return response;
}

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

DefaultHttpClient  httpclient = new DefaultHttpClient();
 httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
   public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
     boolean isRedirect=false;
     try {
       isRedirect = super.isRedirected(request, response, context);
     } catch (ProtocolException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     if (!isRedirect) {
       int responseCode = response.getStatusLine().getStatusCode();
       if (responseCode == 301 || responseCode == 302) {
         return true;
       }
     }
     return isRedirect;
   }
 });

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

@Test
public void connect() throws IOException {
  HttpResponse response = mock(HttpResponse.class);
  when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);
  when(response.getStatusLine()).thenReturn(new BasicStatusLine(HTTP_1_1, 200, "OK"));
  client.connect();
}

代码示例来源: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: robolectric/robolectric

@Test
public void shouldHandleMultipleInvocations() throws Exception {
 FakeHttp.addPendingHttpResponse(200, "a happy response body");
 FakeHttp.addPendingHttpResponse(201, "another happy response body");
 HttpResponse response1 = requestDirector.execute(null, new HttpGet("http://example.com"), null);
 HttpResponse response2 = requestDirector.execute(null, new HttpGet("www.example.com"), null);
 assertThat(response1.getStatusLine().getStatusCode()).isEqualTo(200);
 assertThat(getStringContent(response1)).isEqualTo("a happy response body");
 assertThat(response2.getStatusLine().getStatusCode()).isEqualTo(201);
 assertThat(getStringContent(response2)).isEqualTo("another happy response body");
}

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

/**
   * @param response The http response to verify.
   * @return null on failure or the response string if return code is in 200 range
   */
  @Override
  public String handleResponse(final HttpResponse response) throws IOException {
    int status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();
    String entityString = (entity != null ? EntityUtils.toString(entity) : null);
    if (status >= 200 && status < 300) {
      return entityString;
    } else {
      LOG.error("Got unexpected response code {}; entity: {}", status, entityString);
      return null;
    }
  }
}

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

@Override
public int getStatusCode() {
  return response == null || response.getStatusLine() == null ? 0 : response.getStatusLine().getStatusCode();
}

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

@Override
public void connect() throws IOException {
  HttpPost post = new HttpPost(baseUrl() + "/kylin/api/user/authentication");
  addHttpHeaders(post);
  StringEntity requestEntity = new StringEntity("{}", ContentType.create("application/json", "UTF-8"));
  post.setEntity(requestEntity);
  try {
    HttpResponse response = httpClient.execute(post);
    if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 201) {
      throw asIOException(post, response);
    }
  } finally {
    post.releaseConnection();
  }
}

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

boolean result = false;
   HttpClient hc = new DefaultHttpClient();
   String message;
   HttpPost p = new HttpPost(url);
   JSONObject object = new JSONObject();
   try {
     object.put("updates", updates);
     object.put("mobile", mobile);
     object.put("last_name", lastname);
     object.put("first_name", firstname);
     object.put("email", email);
   } catch (Exception ex) {
   }
   try {
   message = object.toString();
   p.setEntity(new StringEntity(message, "UTF8"));
   p.setHeader("Content-type", "application/json");
     HttpResponse resp = hc.execute(p);
     if (resp != null) {
       if (resp.getStatusLine().getStatusCode() == 204)
         result = true;
     }
     Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
   } catch (Exception e) {
     e.printStackTrace();
   }
   return result;

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

HttpResponse sendHttpRequest(HttpUriRequest req, HttpClient httpClient)
  throws ClientProtocolException, IOException {
 LOG.debug("Sending request {}", req);
 HttpResponse response = httpClient.execute(req);
 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK ||
   null == response.getEntity()) {
  if (response instanceof CloseableHttpResponse) {
   ((CloseableHttpResponse)response).close();
  }
  throw new IOException("HTTP Request " + req + " returned unexpected response " + response);
 }
 return response;
}

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

@Test
public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception {
 FakeHttp.addPendingHttpResponse(200, "a happy response body");
 HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);
 assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
 assertThat(getStringContent(response)).isEqualTo("a happy response body");
}

代码示例来源:origin: chanjarster/weixin-java-tools

public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
 final StatusLine statusLine = response.getStatusLine();
 final HttpEntity entity = response.getEntity();
 if (statusLine.getStatusCode() >= 300) {
  EntityUtils.consume(entity);
  throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
 }
 return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
}

相关文章