Search in sources :

Example 41 with BasicHeader

use of org.apache.http.message.BasicHeader in project android_frameworks_base by crdroidandroid.

the class DefaultHttpClientTest method authenticateDigestAlgorithm.

private void authenticateDigestAlgorithm(String algorithm) throws Exception {
    String challenge = "Digest realm=\"protected area\", " + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", " + "algorithm=" + algorithm;
    DigestScheme digestScheme = new DigestScheme();
    digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
    HttpGet get = new HttpGet();
    digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpGet(org.apache.http.client.methods.HttpGet) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 42 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 43 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 44 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 45 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)

Aggregations

BasicHeader (org.apache.http.message.BasicHeader)233 Header (org.apache.http.Header)120 IOException (java.io.IOException)54 HttpResponse (org.apache.http.HttpResponse)50 Test (org.junit.Test)50 StringEntity (org.apache.http.entity.StringEntity)36 List (java.util.List)25 HashMap (java.util.HashMap)24 URISyntaxException (java.net.URISyntaxException)23 HttpGet (org.apache.http.client.methods.HttpGet)22 StatusLine (org.apache.http.StatusLine)20 HttpPost (org.apache.http.client.methods.HttpPost)20 BasicStatusLine (org.apache.http.message.BasicStatusLine)19 RestResponse (com.google.gerrit.acceptance.RestResponse)18 ArrayList (java.util.ArrayList)18 ProtocolVersion (org.apache.http.ProtocolVersion)18 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)17 File (java.io.File)17 HttpEntity (org.apache.http.HttpEntity)17 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)17