Example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getStatusLine

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getStatusLine.

Prototype

StatusLine getStatusLine();

Source Link

Usage

From source file:com.srotya.tau.ui.BapiLoginDAO.java

public static Entry<String, String> authenticate(String authURL, String username, String password)
        throws Exception {
    CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000);
    HttpPost authRequest = new HttpPost(authURL);
    Gson gson = new Gson();
    JsonObject obj = new JsonObject();
    obj.addProperty(USERNAME, username);
    obj.addProperty(PASSWORD, password);
    StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON);
    authRequest.setEntity(entity);//  w  w  w. j  a v  a 2 s.co m
    CloseableHttpResponse response = client.execute(authRequest);
    if (response.getStatusLine().getStatusCode() == 200) {
        String tokenPair = EntityUtils.toString(response.getEntity());
        JsonArray ary = gson.fromJson(tokenPair, JsonArray.class);
        obj = ary.get(0).getAsJsonObject();
        String token = obj.get(X_SUBJECT_TOKEN).getAsString();
        String hmac = obj.get(HMAC).getAsString();
        return new AbstractMap.SimpleEntry<String, String>(token, hmac);
    } else {
        System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t"
                + response.getStatusLine().getReasonPhrase());
        return null;
    }
}

From source file:PinotResponseTime.java

private static boolean isValid(CloseableHttpResponse res, String path) throws Exception {
    if (res.getStatusLine().getStatusCode() != 200) {
        return false;
    }//from w w w .  j  av a 2  s  .  c  om

    String response = "";
    int length;
    try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
        while ((length = in.read(BUFFER)) > 0) {
            response += new String(BUFFER, 0, length, "UTF-8");
        }
    }
    if (response.contains("\"numDocsScanned\":0")) {
        return false;
    }
    if (path != null) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path, false))) {
            writer.write(response);
        }
    }
    return true;
}

From source file:nayan.netty.client.FileUploadClient.java

private static void uploadFile() throws Exception {
    File file = new File("small.jpg");

    HttpEntity httpEntity = MultipartEntityBuilder.create()
            .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName()).build();

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://localhost:8080");

    httppost.setEntity(httpEntity);//from w w w . ja va2s.  co m
    System.out.println("executing request " + httppost.getRequestLine());

    CloseableHttpResponse response = httpclient.execute(httppost);

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
        System.out.println("Response content length: " + resEntity.getContentLength());
    }

    EntityUtils.consume(resEntity);

    response.close();
}

From source file:com.microsoft.azure.hdinsight.common.StreamUtil.java

public static HttpResponse getResultFromHttpResponse(CloseableHttpResponse response) throws IOException {
    int code = response.getStatusLine().getStatusCode();
    String reason = response.getStatusLine().getReasonPhrase();
    HttpEntity entity = response.getEntity();
    try (InputStream inputStream = entity.getContent()) {
        String response_content = getResultFromInputStream(inputStream);
        return new HttpResponse(code, response_content, new HashMap<String, List<String>>(), reason);
    }// www  .  j  av  a  2  s .  c  o m
}

From source file:com.continuuity.loom.TestHelper.java

public static void finishTask(String loomUrl, FinishTaskRequest finishRequest) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/finish", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(finishRequest)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {//from w  w w  .j ava 2 s  .c  o  m
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        response.close();
    }
}

From source file:com.continuuity.loom.TestHelper.java

public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception {
    HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl));
    httpPost.setEntity(new StringEntity(GSON.toJson(request)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {/*from   www.  j  a v a 2  s  . c o  m*/
        Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100);
        if (response.getEntity() == null) {
            return null;
        }
        return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class);
    } finally {
        response.close();
    }
}

From source file:com.dtstack.jlogstash.distributed.http.cilent.HttpClient.java

public static String post(String url, Map<String, Object> bodyData) {
    String responseBody = null;// w  w  w  .  java  2s.  c  o m
    CloseableHttpClient httpClient = null;
    try {
        httpClient = getHttpClient();
        HttpPost httPost = new HttpPost(url);
        if (SetTimeOut) {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SocketTimeout)
                    .setConnectTimeout(ConnectTimeout).build();//
            httPost.setConfig(requestConfig);
        }
        if (bodyData != null && bodyData.size() > 0) {
            httPost.setEntity(new StringEntity(objectMapper.writeValueAsString(bodyData)));
        }
        //?
        CloseableHttpResponse response = httpClient.execute(httPost);
        int status = response.getStatusLine().getStatusCode();
        if (status == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            //FIXME ?header?
            responseBody = EntityUtils.toString(entity, Charsets.UTF_8);
        } else {
            logger.error("url:" + url + "--->http return status error:" + status);
        }
    } catch (Exception e) {
        logger.error("url:" + url + "--->http request error", e);
    } finally {
        try {
            if (httpClient != null)
                httpClient.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error(ExceptionUtil.getErrorMessage(e));
        }
    }
    return responseBody;
}

From source file:shootersubdownloader.Shootersubdownloader.java

private static void down(File f) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String url = String.format("https://www.shooter.cn/api/subapi.php?filehash=%s&pathinfo=%s&format=json",
            computefilehash(f), f.getName());
    System.out.println(url);/*from w  ww  .j av  a2  s  .  c o  m*/
    HttpGet request = new HttpGet(url);
    CloseableHttpResponse r = httpclient.execute(request);
    System.out.println(r.getStatusLine());
    HttpEntity e = r.getEntity();
    String s = EntityUtils.toString(e);
    System.out.println(s);
    JSONArray json = JSONArray.fromObject(s);
    //        JSONObject json = JSONObject.fromObject(s);
    System.out.println(json.size());
    for (int i = 0; i < json.size(); i++) {
        System.out.println(i);
        JSONObject obj = json.getJSONObject(i);
        JSONArray fs = obj.getJSONArray("Files");
        String downurl = fs.getJSONObject(0).getString("Link");
        HttpGet r2 = new HttpGet(downurl);
        CloseableHttpResponse res2 = httpclient.execute(r2);
        //            Header[] headers = res2.getAllHeaders();
        //            for(Header h:headers){
        //                System.out.println(h.getName());
        //                System.out.println(h.getValue());
        //            }
        Header header = res2.getFirstHeader("Content-Disposition");
        String sig = "filename=";
        String v = header.getValue();
        String fn = v.substring(v.indexOf(sig) + sig.length());
        HttpEntity e2 = res2.getEntity();
        File outf = new File(fn);
        FileOutputStream fos = new FileOutputStream(outf);
        e2.writeTo(fos);

        System.out.println(filecharsetdetect.FileCharsetDetect.detect(outf));
        //            res2.getEntity().writeTo(new FileOutputStream(fn));
        System.out.println(fn);
        res2.close();
    }

    r.close();
    httpclient.close();
}

From source file:com.temenos.useragent.generic.http.DefaultHttpClientHelper.java

/**
 * Builds and returns http interaction execution result.
 * //from w w w.  ja  va  2s. c  om
 * @param httpResponse
 * @return interaction execution result
 */
public static Result buildResult(CloseableHttpResponse httpResponse) {
    StatusLine statusLine = httpResponse.getStatusLine();
    return new HttpResult(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}

From source file:org.rapidoid.http.HTTP.java

public static byte[] get(String uri) {
    try {/*from   w  ww. j a v  a  2 s  .  com*/
        CloseableHttpClient client = client(uri);

        HttpGet get = new HttpGet(uri);

        Log.info("Starting HTTP GET request", "request", get.getRequestLine());

        CloseableHttpResponse response = client.execute(get);

        int statusCode = response.getStatusLine().getStatusCode();
        U.must(statusCode == 200, "Expected HTTP status code 200, but found: %s", statusCode);

        InputStream resp = response.getEntity().getContent();

        return IOUtils.toByteArray(resp);
    } catch (Throwable e) {
        throw U.rte(e);
    }
}