Search in sources :

Example 51 with BasicHeader

use of org.apache.http.message.BasicHeader in project CodeUtils by boredream.

the class HttpUtils method getOrPostFile.

public static byte[] getOrPostFile(int method, String url, Map<String, String> postParams, Map<String, String> headers) throws Exception {
    HashMap<String, String> map = new HashMap<String, String>();
    if (headers != null) {
        map.putAll(headers);
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, method, postParams);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    return bytes;
}
Also used : HashMap(java.util.HashMap) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 52 with BasicHeader

use of org.apache.http.message.BasicHeader in project Gargoyle by callakrsos.

the class RequestUtilTest method gridInfo.

@Test
public void gridInfo() throws Exception {
    HashMap<String, String> data = new HashMap<String, String>();
    Header[] headers = { // PortalhomelastLoginCheck=Y; QSN_SKIP=Y"),
    new BasicHeader("Cookie", "_pdss_ckinfo=2f2f946ca433cdc76f44284ba7c3439254b37cadb63d26ab69bb5aa0aa36661b0c29c995b1b4e6c3da1a5a9b48b18678; " + "cert=365e5ee2fde6b9ca8be8beb1d586fd6e2b18f043bc9aa96f80416e62abcf0b78^uGtB8XGYs7Qai3XkaDWexg==; " + "lat=6322d147370012504b81988cd55628e32b18f043bc9aa96f80416e62abcf0b78; " + "JSESSIONID=jpn6YSvXTBxnbL34x5Kgs73JNbfG2r6WMgqvQ25p2CdZjHBycnJQ!-1149058482; " + "id=4fdcd939a24d97651e1ebbe6afe265e0e3ddf94c144bbf16e1b61b6c5cac390c6c9d5cad694b44c6dbbf90d924d449e8eb846bd33aaeb878da1a5a9b48b18678; " + "pagelat=7e191099de456767322e3700dc7c35c7f25666fe77429de0da1a5a9b48b18678; " + "PortalhomelastLoginCheck=Y; " + "QSN_SKIP=Y; ") };
    // FIXED
    // PortalhomelastLoginCheck=Y; QSN_SKIP=Y
    // pagelat=7e191099de456767322e3700dc7c35c7f25666fe77429de0da1a5a9b48b18678;
    // headers = null;
    // "usertypecd=1&s_locatecd=A&s_deptcddeptposyn=Y&workdtStartView=2017.03.20&workdtStart=20170320&workdtEndView=2017.03.21&workdtEnd=20170321&s_locatecd=A"
    /// GridCodeRetrieveAction.do
    // GTPortalFlextimeMgmtRetrieve.do
    String request = RequestUtil.CookieBase.request("http://70.20.1.140:8501/GTPortalFlextimeMgmtRetrieve.do?" + "isIBSheet=Y&" + "_isUseZip=N&" + "pageInfo_pageid=gtx0350&" + "pageInfo_seqno=0&" + "pageInfo_locatecd=&" + "pageInfo_authsik=f82b861e0520d14c894a539512ffa0a5b815eea870cfa50e118fc571c867d82a376e4c743aa44055f67fb88aaf1e44efcd94657cc59ae4e78c1cd8d971ba49d33ee85f49db6e4ce680416e62abcf0b78&" + "pageInfo_authdeptsik=13e87f568d5bc2ad46d3f9dbd07d8c9a80416e62abcf0b78&" + "pageInfo_tgtsik=13e87f568d5bc2ad46d3f9dbd07d8c9a80416e62abcf0b78&" + "pageInfo_actyn=N&" + "pageInfo_locategbncd=SA313&" + "pageInfo_retlogyn=N&" + "pageInfo_updlogyn=N&" + "pageInfo_pagetypecd=1&" + "pageInfo_datasource=&" + "pageInfo_authemp=&" + "pageInfo_localgbn=&" + "empinfotype0=empinfogt8&" + "usertypecd=1&" + "s_locatecd=A&" + "s_deptcddeptposyn=Y&" + "workdtStartView=2017.03.20&" + "workdtStart=20170320&" + "workdtEndView=2017.03.21&" + "workdtEnd=20170321&" + "s_locatecd=A", headers, data, (is, code) -> {
        byte[] b = new byte[4096];
        StringBuffer sb = new StringBuffer();
        try {
            while (is.read(b) != -1) {
                sb.append(new String(b, 0, 4096, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    });
    System.out.println(request);
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HashMap(java.util.HashMap) BasicHeader(org.apache.http.message.BasicHeader) ClientProtocolException(org.apache.http.client.ClientProtocolException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 53 with BasicHeader

use of org.apache.http.message.BasicHeader in project CodeUtils by boredream.

the class LeanCloudHttpUtils method postFile.

public static String postFile(String url, File file) throws Exception {
    // LeanCloud上传限制, 最多1秒1个
    Thread.sleep(1000);
    HashMap<String, String> map = getHeaderMap();
    map.put("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    int len;
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(file);
    while ((len = fis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 54 with BasicHeader

use of org.apache.http.message.BasicHeader in project CodeUtils by boredream.

the class BmobHttpUtils method getOrPostString.

private static String getOrPostString(int method, String url, Map<String, String> postParams) throws Exception {
    HashMap<String, String> map = new HashMap<String, String>();
    // bmob header
    map.put("X-Bmob-Application-Id", APP_ID);
    map.put("X-Bmob-REST-API-Key", REST_API_KEY);
    map.put("Content-Type", "application/json");
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, method, postParams);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 55 with BasicHeader

use of org.apache.http.message.BasicHeader in project CodeUtils by boredream.

the class LeanCloudHttpUtils method postBean.

public static String postBean(Object object) throws Exception {
    String url = "https://api.leancloud.cn/1.1/classes/";
    url += object.getClass().getSimpleName();
    HashMap<String, String> map = getHeaderMap();
    map.put("Content-Type", HEADER_CONTENT_TYPE);
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", HEADER_CONTENT_TYPE);
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    out.write(new Gson().toJson(object).getBytes());
    out.close();
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    return new String(bytes, responseCharset);
}
Also used : Gson(com.google.gson.Gson) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

BasicHeader (org.apache.http.message.BasicHeader)128 Header (org.apache.http.Header)67 Test (org.junit.Test)29 List (java.util.List)21 HashMap (java.util.HashMap)19 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicStatusLine (org.apache.http.message.BasicStatusLine)17 ProtocolVersion (org.apache.http.ProtocolVersion)16 StatusLine (org.apache.http.StatusLine)16 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 URL (java.net.URL)15 HttpResponse (org.apache.http.HttpResponse)15 Response (org.elasticsearch.client.Response)10 ArrayList (java.util.ArrayList)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 TestHttpResponse (org.robolectric.shadows.httpclient.TestHttpResponse)9 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)8 StringEntity (org.apache.http.entity.StringEntity)8