Search in sources :

Example 96 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project platform_external_apache-http by android.

the class BasicLineParser method parseRequestLine.

/**
     * Parses a request line.
     *
     * @param buffer    a buffer holding the line to parse
     *
     * @return  the parsed request line
     *
     * @throws ParseException        in case of a parse error
     */
public RequestLine parseRequestLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String method = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        skipWhitespace(buffer, cursor);
        i = cursor.getPos();
        blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String uri = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        skipWhitespace(buffer, cursor);
        if (!cursor.atEnd()) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        return createRequestLine(method, uri, ver);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 97 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project platform_external_apache-http by android.

the class HttpRequestBase method getRequestLine.

public RequestLine getRequestLine() {
    String method = getMethod();
    ProtocolVersion ver = getProtocolVersion();
    URI uri = getURI();
    String uritext = null;
    if (uri != null) {
        uritext = uri.toASCIIString();
    }
    if (uritext == null || uritext.length() == 0) {
        uritext = "/";
    }
    return new BasicRequestLine(method, uritext, ver);
}
Also used : BasicRequestLine(org.apache.http.message.BasicRequestLine) ProtocolVersion(org.apache.http.ProtocolVersion) URI(java.net.URI)

Example 98 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class HttpUtils method postFile.

private static String postFile(String url, File file, Map<String, String> headers) throws Exception {
    HashMap<String, String> map = new HashMap<>();
    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));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty(HEADER_CONTENT_TYPE, getBodyContentType());
    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 : 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 99 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class HttpUtils method getOrPostString.

private static String getOrPostString(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());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
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 100 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project CodeUtils by boredream.

the class LeanCloudHttpUtils method getOrPostString.

private static String getOrPostString(int method, String url, Map<String, String> postParams) throws Exception {
    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));
    }
    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 : 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

ProtocolVersion (org.apache.http.ProtocolVersion)105 BasicStatusLine (org.apache.http.message.BasicStatusLine)46 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)43 StatusLine (org.apache.http.StatusLine)42 Header (org.apache.http.Header)26 HttpEntity (org.apache.http.HttpEntity)26 Test (org.junit.Test)25 HttpResponse (org.apache.http.HttpResponse)22 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)14 HttpHost (org.apache.http.HttpHost)12 ParseException (org.apache.http.ParseException)12 StringEntity (org.apache.http.entity.StringEntity)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 MockHttpStack (com.android.volley.mock.MockHttpStack)10 HttpRequest (org.apache.http.HttpRequest)10