Search in sources :

Example 16 with ProtocolException

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

the class RequestTargetHost 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 (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (!request.containsHeader(HTTP.TARGET_HOST)) {
        HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (targethost == null) {
            HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
            if (conn instanceof HttpInetConnection) {
                // Populate the context with a default HTTP host based on the 
                // inet address of the target host
                InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
                int port = ((HttpInetConnection) conn).getRemotePort();
                if (address != null) {
                    targethost = new HttpHost(address.getHostName(), port);
                }
            }
            if (targethost == null) {
                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
                if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                    return;
                } else {
                    throw new ProtocolException("Target host missing");
                }
            }
        }
        request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpConnection(org.apache.http.HttpConnection) HttpHost(org.apache.http.HttpHost) HttpInetConnection(org.apache.http.HttpInetConnection) ProtocolVersion(org.apache.http.ProtocolVersion) InetAddress(java.net.InetAddress)

Example 17 with ProtocolException

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

the class DefaultResponseParser method parseHead.

@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    // clear the buffer
    this.lineBuf.clear();
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;
    do {
        int i = sessionBuffer.readLine(this.lineBuf);
        if (i == -1 && count == 0) {
            // The server just dropped connection on us
            throw new NoHttpResponseException("The target server failed to respond");
        }
        cursor = new ParserCursor(0, this.lineBuf.length());
        if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
            // Got one
            break;
        } else if (i == -1 || count >= this.maxGarbageLines) {
            // Giving up
            throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
        }
        count++;
    } while (true);
    //create the status line from the status string
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ProtocolException(org.apache.http.ProtocolException)

Example 18 with ProtocolException

use of org.apache.http.ProtocolException in project ABPlayer by winkstu.

the class HttpUtil method GetCookie.

public static Integer GetCookie(String url, String number, String pw, String select, String host) {
    System.out.println("GetCookie");
    int result = 4;
    HttpPost httpPost = new HttpPost(hostBase + url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("number", number));
    nvps.add(new BasicNameValuePair("passwd", pw));
    nvps.add(new BasicNameValuePair("select", select));
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
        httpClient.setRedirectHandler(new RedirectHandler() {

            @Override
            public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                return false;
            }

            @Override
            public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
                return null;
            }
        });
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            return 2;
        } else if (response.getStatusLine().getStatusCode() == 302) {
            Header[] headers = response.getHeaders("Location");
            if (headers != null && headers.length > 0) {
                List<Cookie> list = httpClient.getCookieStore().getCookies();
                for (Cookie c : list) {
                    cookieName = c.getName();
                    cookieValue = c.getValue();
                }
                System.out.println(cookieName + cookieValue);
                return 3;
            }
        } else if (response.getStatusLine().getStatusCode() == 404) {
            return -1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : Cookie(org.apache.http.cookie.Cookie) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) RedirectHandler(org.apache.http.client.RedirectHandler) ArrayList(java.util.ArrayList) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) DataFormatException(java.util.zip.DataFormatException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) List(java.util.List) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 19 with ProtocolException

use of org.apache.http.ProtocolException in project ABPlayer by winkstu.

the class HttpUtil method getCookie.

public static int getCookie(String url) {
    System.out.println("getCookie" + url);
    HttpGet httpGet = new HttpGet(hostBase + url);
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setRedirectHandler(new RedirectHandler() {

            @Override
            public boolean isRedirectRequested(HttpResponse response, HttpContext context) {
                return false;
            }

            @Override
            public URI getLocationURI(HttpResponse response, HttpContext context) throws ProtocolException {
                return null;
            }
        });
        HttpResponse response = httpClient.execute(httpGet);
        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
        if (response.getStatusLine().getStatusCode() == 200) {
            Header[] heads = response.getAllHeaders();
            System.out.println(heads.length);
            for (Header header : heads) {
                System.out.println(header.getName() + " = " + header.getValue());
            }
            return 2;
        } else if (response.getStatusLine().getStatusCode() == 302) {
            Header[] headers = response.getHeaders("Location");
            if (headers != null && headers.length > 0) {
                System.out.println(headers[0].getValue());
                return 3;
            }
        } else if (response.getStatusLine().getStatusCode() == 404) {
            return -1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 1;
}
Also used : ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Header(org.apache.http.Header) RedirectHandler(org.apache.http.client.RedirectHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) DataFormatException(java.util.zip.DataFormatException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Example 20 with ProtocolException

use of org.apache.http.ProtocolException in project undertow by undertow-io.

the class SaveOriginalPostRequestTestCase method createHttpClient.

private TestHttpClient createHttpClient() {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    return client;
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) HttpContext(org.apache.http.protocol.HttpContext) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient)

Aggregations

ProtocolException (org.apache.http.ProtocolException)52 Header (org.apache.http.Header)21 URI (java.net.URI)16 ArrayList (java.util.ArrayList)16 HttpHost (org.apache.http.HttpHost)14 HttpRequest (org.apache.http.HttpRequest)13 HttpResponse (org.apache.http.HttpResponse)13 HttpContext (org.apache.http.protocol.HttpContext)13 URISyntaxException (java.net.URISyntaxException)11 HttpPost (org.apache.http.client.methods.HttpPost)11 NameValuePair (org.apache.http.NameValuePair)10 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)10 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 ParseException (org.apache.http.ParseException)9 ProtocolVersion (org.apache.http.ProtocolVersion)9 DefaultRedirectStrategy (org.apache.http.impl.client.DefaultRedirectStrategy)9 Test (org.junit.Test)8 IOException (java.io.IOException)7 HttpGet (org.apache.http.client.methods.HttpGet)7