Search in sources :

Example 76 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams 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 77 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams 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)

Example 78 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project newsrob by marianokamp.

the class CountingInputStream method newInstance.

public static NewsRobHttpClient newInstance(boolean followRedirects, Context ctx) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 45 * 1000);
    HttpConnectionParams.setSoTimeout(params, 45 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // HttpConnectionParams.setTcpNoDelay(params, true);
    // Don't handle redirects -- return them to the caller. Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    // HttpClientParams.setRedirecting(params, false);
    HttpClientParams.setRedirecting(params, followRedirects);
    if (followRedirects)
        params.setIntParameter(ClientPNames.MAX_REDIRECTS, 10);
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, USER_AGENT);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new NewsRobHttpClient(manager, params, ctx);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 79 with BasicHttpParams

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

the class DefaultHttpClient method createHttpParams.

@Override
protected HttpParams createHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    /*
         * Android note: Send each request body without first asking the server
         * whether it will be accepted. Asking first slows down the common case
         * and results in "417 expectation failed" errors when a HTTP/1.0 server
         * is behind a proxy. http://b/2471595
         */
    HttpProtocolParams.setUseExpectContinue(params, // android-changed
    false);
    // determine the release version from packaged version info
    final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader());
    final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
    HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.4)");
    return params;
}
Also used : VersionInfo(org.apache.http.util.VersionInfo) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Example 80 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project tdi-studio-se by Talend.

the class DeviceIdManager method executeRegistrationRequest.

/**
     * This method demonstrate execution of device registration request.
     * 
     * @param url Registration uri
     * @param soapEnvelope Registration xml envelope
     * @return device registration puid
     */
private static String executeRegistrationRequest(String url, String soapEnvelope) throws IllegalStateException, SAXException, ParserConfigurationException, DeviceRegistrationFailedException, IOException {
    HttpResponse response = null;
    // Create the request that will submit the request to the server
    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 180000);
        HttpClient client = new SystemDefaultHttpClient(params);
        // Uncomment following lines to view the traffic in fiddler.
        // HttpHost proxy = new HttpHost("localhost", 8888);
        // client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(soapEnvelope);
        post.setHeader("Content-Type", "application/soap+xml; charset=UTF-8");
        post.setEntity(entity);
        response = client.execute(post);
        return parseRegistrationResponse(response, false);
    } catch (ClientProtocolException e) {
        Log.error(e.getMessage());
        parseRegistrationResponse(response, true);
        throw e;
    } catch (IOException e) {
        Log.error(e.getMessage());
        parseRegistrationResponse(response, true);
        throw e;
    }
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpClient(org.apache.http.client.HttpClient) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

BasicHttpParams (org.apache.http.params.BasicHttpParams)82 HttpParams (org.apache.http.params.HttpParams)62 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)47 IOException (java.io.IOException)33 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)31 Scheme (org.apache.http.conn.scheme.Scheme)30 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)29 HttpResponse (org.apache.http.HttpResponse)27 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)24 ClientProtocolException (org.apache.http.client.ClientProtocolException)15 HttpClient (org.apache.http.client.HttpClient)12 HttpGet (org.apache.http.client.methods.HttpGet)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 Socket (java.net.Socket)11 HttpPost (org.apache.http.client.methods.HttpPost)10 ConnectException (java.net.ConnectException)8 KeyManagementException (java.security.KeyManagementException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 SSLContext (javax.net.ssl.SSLContext)8 StringEntity (org.apache.http.entity.StringEntity)8