Search in sources :

Example 11 with Header

use of org.apache.http.Header in project android_frameworks_base by ParanoidAndroid.

the class AndroidHttpClient method toCurl.

/**
     * Generates a cURL command equivalent to the given request.
     */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();
    builder.append("curl ");
    // add in the method
    builder.append("-X ");
    builder.append(request.getMethod());
    builder.append(" ");
    for (Header header : request.getAllHeaders()) {
        if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }
    URI uri = request.getURI();
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }
    builder.append("\"");
    builder.append(uri);
    builder.append("\"");
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                if (isBinaryContent(request)) {
                    String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
                    builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
                    builder.append(" --data-binary @/tmp/$$.bin");
                } else {
                    String entityString = stream.toString();
                    builder.append(" --data-ascii \"").append(entityString).append("\"");
                }
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }
    return builder.toString();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Header(org.apache.http.Header) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestWrapper(org.apache.http.impl.client.RequestWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI)

Example 12 with Header

use of org.apache.http.Header in project android_frameworks_base by ParanoidAndroid.

the class MultipartTest method testParts.

public void testParts() throws Exception {
    StringBuffer filebuffer = new StringBuffer();
    String filepartStr = "this is file part";
    filebuffer.append(filepartStr);
    File upload = File.createTempFile("Multipart", "test");
    FileWriter outFile = new FileWriter(upload);
    BufferedWriter out = new BufferedWriter(outFile);
    try {
        out.write(filebuffer.toString());
        out.flush();
    } finally {
        out.close();
    }
    Part[] parts = new Part[3];
    parts[0] = new StringPart("stringpart", "PART1!!");
    parts[1] = new FilePart(upload.getName(), upload);
    parts[2] = new StringPart("stringpart", "PART2!!");
    MultipartEntity me = new MultipartEntity(parts);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    me.writeTo(os);
    Header h = me.getContentType();
    String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary());
    StringBuffer contentType = new StringBuffer("multipart/form-data");
    contentType.append("; boundary=");
    contentType.append(boundry);
    assertEquals("Multipart content type error", contentType.toString(), h.getValue());
    final String CRLF = "\r\n";
    StringBuffer output = new StringBuffer();
    output.append("--");
    output.append(boundry);
    output.append(CRLF);
    output.append("Content-Disposition: form-data; name=\"stringpart\"");
    output.append(CRLF);
    output.append("Content-Type: text/plain; charset=US-ASCII");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: 8bit");
    output.append(CRLF);
    output.append(CRLF);
    output.append("PART1!!");
    output.append(CRLF);
    output.append("--");
    output.append(boundry);
    output.append(CRLF);
    output.append("Content-Disposition: form-data; name=\"");
    output.append(upload.getName());
    output.append("\"; filename=\"");
    output.append(upload.getName());
    output.append("\"");
    output.append(CRLF);
    output.append("Content-Type: application/octet-stream; charset=ISO-8859-1");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: binary");
    output.append(CRLF);
    output.append(CRLF);
    output.append(filepartStr);
    output.append(CRLF);
    output.append("--");
    output.append(boundry);
    output.append(CRLF);
    output.append("Content-Disposition: form-data; name=\"stringpart\"");
    output.append(CRLF);
    output.append("Content-Type: text/plain; charset=US-ASCII");
    output.append(CRLF);
    output.append("Content-Transfer-Encoding: 8bit");
    output.append(CRLF);
    output.append(CRLF);
    output.append("PART2!!");
    output.append(CRLF);
    output.append("--");
    output.append(boundry);
    output.append("--");
    output.append(CRLF);
    // System.out.print(output.toString());
    assertEquals("Multipart content error", output.toString(), os.toString());
// System.out.print(os.toString());
}
Also used : Header(org.apache.http.Header) FileWriter(java.io.FileWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 13 with Header

use of org.apache.http.Header in project UltimateAndroid by cymcsg.

the class HttpUtils_Deprecated method GetJson_Cookie.

/**
     * 处理httpResponse信息,返回json,保存cookie
     *
     * @param
     * @return String
     */
public static String GetJson_Cookie(String httpUrl) {
    String strResult = null;
    try {
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(httpUrl);
        // 取得HttpClient对象
        HttpClient httpclient = new DefaultHttpClient();
        // 请求HttpClient,取得HttpResponse
        HttpResponse httpResponse = httpclient.execute(httpRequest);
        //保存cookie
        StringBuffer sb = new StringBuffer();
        String inputLine = "";
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            Header[] hds = httpResponse.getAllHeaders();
            int isok = 0;
            for (int index = 0; index < hds.length; index++) {
                if ("Set-Cookie".equals(hds[index].getName())) {
                    String value = hds[index].getValue();
                    String[] vs = value.split(";");
                    for (int i = 0; i < vs.length; i++) {
                        String[] vss = vs[i].split("=");
                        if ("member".equals(vss[0])) {
                            rst = vs[i] + ";";
                            Log.d("Chen", "cookie信息:" + rst);
                            isok++;
                        }
                    }
                }
            }
        }
        // 请求成功
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // 取得返回的字符串
            strResult = EntityUtils.toString(httpResponse.getEntity());
        } else {
            Log.i(TAG, "请求错误");
        }
    } catch (Exception e) {
        Log.i(TAG, "请求网络异常");
        strResult = "net_ex";
    }
    return strResult;
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) MalformedURLException(java.net.MalformedURLException)

Example 14 with Header

use of org.apache.http.Header in project hive by apache.

the class HttpBasicAuthInterceptor method addHttpAuthHeader.

@Override
protected void addHttpAuthHeader(HttpRequest httpRequest, HttpContext httpContext) throws Exception {
    Header basicAuthHeader = authScheme.authenticate(credentials, httpRequest, httpContext);
    httpRequest.addHeader(basicAuthHeader);
}
Also used : Header(org.apache.http.Header)

Example 15 with Header

use of org.apache.http.Header in project spark by perwendel.

the class SparkTestUtil method doMethod.

public UrlResponse doMethod(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) throws IOException {
    HttpUriRequest httpRequest = getHttpRequest(requestMethod, path, body, secureConnection, acceptType, reqHeaders);
    HttpResponse httpResponse = httpClient.execute(httpRequest);
    UrlResponse urlResponse = new UrlResponse();
    urlResponse.status = httpResponse.getStatusLine().getStatusCode();
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        urlResponse.body = EntityUtils.toString(entity);
    } else {
        urlResponse.body = "";
    }
    Map<String, String> headers = new HashMap<>();
    Header[] allHeaders = httpResponse.getAllHeaders();
    for (Header header : allHeaders) {
        headers.put(header.getName(), header.getValue());
    }
    urlResponse.headers = headers;
    return urlResponse;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse)

Aggregations

Header (org.apache.http.Header)906 HttpResponse (org.apache.http.HttpResponse)368 HttpGet (org.apache.http.client.methods.HttpGet)253 Test (org.junit.Test)206 IOException (java.io.IOException)200 BasicHeader (org.apache.http.message.BasicHeader)160 HttpEntity (org.apache.http.HttpEntity)134 TestHttpClient (io.undertow.testutils.TestHttpClient)94 URI (java.net.URI)93 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)93 ArrayList (java.util.ArrayList)90 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HashMap (java.util.HashMap)76 InputStream (java.io.InputStream)67 URISyntaxException (java.net.URISyntaxException)64 HttpPost (org.apache.http.client.methods.HttpPost)62 StatusLine (org.apache.http.StatusLine)60 List (java.util.List)52 StringEntity (org.apache.http.entity.StringEntity)51 HttpHost (org.apache.http.HttpHost)46