Search in sources :

Example 1 with RedirectHandler

use of org.apache.http.client.RedirectHandler 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 2 with RedirectHandler

use of org.apache.http.client.RedirectHandler 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 3 with RedirectHandler

use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.

the class HttpUtil method httpGetCookie.

public static String httpGetCookie(String url) {
    System.out.println("httpGetCookie" + url);
    HttpGet httpget = new HttpGet(hostBase + url);
    String strResult = "";
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
    HttpConnectionParams.setSoTimeout(httpParams, 15000);
    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;
            }
        });
        httpget.setHeader("Cookie", cookieName + "=" + cookieValue);
        HttpResponse response = httpClient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        } else if (response.getStatusLine().getStatusCode() == 302) {
            // cookieʧЧ�������ض����־�������µ�¼��ȡ
            strResult = "302";
        } else if (response.getStatusLine().getStatusCode() == 404) {
            strResult = "-1";
        }
    } catch (Exception e) {
        e.printStackTrace();
        strResult = "4";
    }
    return strResult;
}
Also used : ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) RedirectHandler(org.apache.http.client.RedirectHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) 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 4 with RedirectHandler

use of org.apache.http.client.RedirectHandler in project ABPlayer by winkstu.

the class HttpUtil method httpPostCookie.

public static String httpPostCookie(String url, String id, String data) {
    System.out.println("httpPostCookie" + url);
    String result = "4";
    HttpPost httpPost = new HttpPost(hostBase + url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("marc_no", id));
    nvps.add(new BasicNameValuePair("r_content", data));
    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.setHeader("Cookie", cookieName + "=" + cookieValue);
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println(EntityUtils.toString(response.getEntity(), HTTP.UTF_8) + "add");
        if (response.getStatusLine().getStatusCode() == 200) {
            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 result;
}
Also used : 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) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Aggregations

IOException (java.io.IOException)4 ConnectException (java.net.ConnectException)4 URI (java.net.URI)4 DataFormatException (java.util.zip.DataFormatException)4 HttpResponse (org.apache.http.HttpResponse)4 ProtocolException (org.apache.http.ProtocolException)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 RedirectHandler (org.apache.http.client.RedirectHandler)4 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)4 HttpContext (org.apache.http.protocol.HttpContext)4 BasicHttpParams (org.apache.http.params.BasicHttpParams)3 ArrayList (java.util.ArrayList)2 NameValuePair (org.apache.http.NameValuePair)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 List (java.util.List)1 Header (org.apache.http.Header)1 Cookie (org.apache.http.cookie.Cookie)1