Search in sources :

Example 21 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

the class RequestWrapper method getRequestLine.

public RequestLine getRequestLine() {
    String method = getMethod();
    ProtocolVersion ver = getProtocolVersion();
    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)

Example 22 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robolectric by robolectric.

the class DefaultRequestDirector method createConnectRequest.

/**
   * Creates the CONNECT request for tunnelling.
   * Called by {@link #createTunnelToTarget createTunnelToTarget}.
   *
   * @param route     the route to establish
   * @param context   the context for request execution
   *
   * @return  the CONNECT request for tunnelling
   */
protected HttpRequest createConnectRequest(HttpRoute route, HttpContext context) {
    // see RFC 2817, section 5.2 and
    // INTERNET-DRAFT: Tunneling TCP based protocols through
    // Web proxy servers
    HttpHost target = route.getTargetHost();
    String host = target.getHostName();
    int port = target.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().getScheme(target.getSchemeName());
        port = scheme.getDefaultPort();
    }
    StringBuilder buffer = new StringBuilder(host.length() + 6);
    buffer.append(host);
    buffer.append(':');
    buffer.append(Integer.toString(port));
    String authority = buffer.toString();
    ProtocolVersion ver = HttpProtocolParams.getVersion(params);
    HttpRequest req = new BasicHttpRequest("CONNECT", authority, ver);
    return req;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) Scheme(org.apache.http.conn.scheme.Scheme) AuthScheme(org.apache.http.auth.AuthScheme) HttpHost(org.apache.http.HttpHost) ProtocolVersion(org.apache.http.ProtocolVersion) BasicHttpRequest(org.apache.http.message.BasicHttpRequest)

Example 23 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project iosched by google.

the class HurlStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // Signal to the caller that something was wrong with the 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);
        }
    }
    return response;
}
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 24 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project iosched by google.

the class MockHttpClient method execute.

// This is the only one we actually use.
@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) {
    requestExecuted = request;
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), mStatusCode, "");
    HttpResponse response = new BasicHttpResponse(statusLine);
    response.setEntity(mResponseEntity);
    return response;
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 25 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class RequestContent method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (request instanceof HttpEntityEnclosingRequest) {
        if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (request.containsHeader(HTTP.CONTENT_LEN)) {
            throw new ProtocolException("Content-Length header already present");
        }
        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity == null) {
            request.addHeader(HTTP.CONTENT_LEN, "0");
            return;
        }
        // Must specify a transfer encoding or a content length 
        if (entity.isChunked() || entity.getContentLength() < 0) {
            if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
            }
            request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
        } else {
            request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
        }
        // Specify a content type if known
        if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE)) {
            request.addHeader(entity.getContentType());
        }
        // Specify a content encoding if known
        if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
            request.addHeader(entity.getContentEncoding());
        }
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ProtocolVersion(org.apache.http.ProtocolVersion)

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