Search in sources :

Example 81 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project UltimateAndroid by cymcsg.

the class HttpUtils_Deprecated method getResponseStatusCodeFromPostUrl.

/*
     * 发送POST请求,通过URL和参数获取服务器反馈应答,通过返回的应答对象 在对数据头进行分析(如获得报文头 报文体等)
     */
public static Map getResponseStatusCodeFromPostUrl(String url, String logininfo, List<NameValuePair> params) throws Exception {
    Map rstmap = new HashMap();
    if (url.contains("http")) {
    } else {
        url = "http://t.qingdaonews.com" + url;
    }
    HttpPost httpRequest = new HttpPost(url);
    httpRequest.addHeader("Cookie", logininfo);
    if (null != params && params.size() > 0) {
        /* 添加请求参数到请求对象 */
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    }
    HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
    StringBuffer sb = new StringBuffer();
    String inputLine = "";
    rstmap.put("code", httpResponse.getStatusLine().getStatusCode());
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        InputStreamReader is = new InputStreamReader(httpResponse.getEntity().getContent());
        BufferedReader in = new BufferedReader(is);
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
        }
        in.close();
        rstmap.put("content", sb.toString());
    } else {
        rstmap.put("content", null);
    }
    return rstmap;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HashMap(java.util.HashMap) Map(java.util.Map) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 82 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project UltimateAndroid by cymcsg.

the class CommonHttpClient method getNewInstance.

// 每次返回同一实例
// public static synchronized HttpClient getInstance(Context mContext){
//
// if(null == singleStance){
// singleStance = getNewInstance(mContext);
// }
// return singleStance ;
// }
// 每次都返回新的HttpClient实例
public static HttpClient getNewInstance(Context mContext) {
    HttpClient newInstance;
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    // 自定义三个timeout参数
    /*
         * 1.set a timeout for the connection manager,it defines how long we
		 * should wait to get a connection out of the connection pool managed by
		 * the connection manager
		 */
    ConnManagerParams.setTimeout(params, 5000);
    /*
         * 2.The second timeout value defines how long we should wait to make a
		 * connection over the network to the server on the other end
		 */
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
    /*
         * 3.we set a socket timeout value to 4 seconds to define how long we
		 * should wait to get data back for our request.
		 */
    HttpConnectionParams.setSoTimeout(params, TIMEOUT_SOCKET);
    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    newInstance = new DefaultHttpClient(conMgr, params);
    switch(checkNetworkTypeDeprecated(mContext)) {
        case TYPE_CT_WAP:
            {
                // 通过代理解决中国移动联通GPRS中wap无法访问的问题
                HttpHost proxy = new HttpHost("10.0.0.200", 80, "http");
                newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.200访问www");
            }
            break;
        case TYPE_CM_CU_WAP:
            {
                // 通过代理解决中国移动联通GPRS中wap无法访问的问题
                HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
                newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
                Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.172访问www");
            }
            break;
    }
    return newInstance;
}
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) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 83 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project UltimateAndroid by cymcsg.

the class HttpUtils method uploadFilesMPE.

/**
     * Update Files via Multipart
     *
     * @param url
     * @param paramsList
     * @param fileParams
     * @param file
     * @param files
     * @return status
     * @deprecated
     */
public static String uploadFilesMPE(String url, List<NameValuePair> paramsList, String fileParams, File file, File... files) {
    String result = "";
    try {
        DefaultHttpClient mHttpClient;
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);
        mHttpClient = new DefaultHttpClient(params);
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        if (BasicUtils.judgeNotNull(paramsList)) {
            for (NameValuePair nameValuePair : paramsList) {
                multipartEntity.addPart(nameValuePair.getName(), new StringBody(nameValuePair.getValue()));
            }
        }
        multipartEntity.addPart(fileParams, new FileBody(file));
        for (File f : files) {
            multipartEntity.addPart(fileParams, new FileBody(f));
        }
        httpPost.setEntity(multipartEntity);
        HttpResponse httpResp = mHttpClient.execute(httpPost);
        // 判断是够请求成功
        if (httpResp.getStatusLine().getStatusCode() == 200) {
            // 获取返回的数据
            result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
            Logs.d("HttpPost success :");
            Logs.d(result);
        } else {
            Logs.d("HttpPost failed" + "    " + httpResp.getStatusLine().getStatusCode() + "   " + EntityUtils.toString(httpResp.getEntity(), "UTF-8"));
            result = "HttpPost failed";
        }
    } catch (ConnectTimeoutException e) {
        result = "ConnectTimeoutException";
        Logs.e("HttpPost overtime:  " + "");
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
        result = "Exception";
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FileBody(org.apache.http.entity.mime.content.FileBody) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 84 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project cw-android by commonsguy.

the class Downloader method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    client = new DefaultHttpClient();
}
Also used : DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 85 with DefaultHttpClient

use of org.apache.http.impl.client.DefaultHttpClient in project Java-Mandrill-Wrapper by cribbstechnologies.

the class UrlsTest method before.

@Before
public void before() {
    client = new DefaultHttpClient();
    request.setHttpClient(client);
}
Also used : DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Before(org.junit.Before)

Aggregations

DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)342 HttpResponse (org.apache.http.HttpResponse)199 HttpGet (org.apache.http.client.methods.HttpGet)167 HttpClient (org.apache.http.client.HttpClient)139 IOException (java.io.IOException)104 Test (org.junit.Test)65 HttpPost (org.apache.http.client.methods.HttpPost)64 HttpEntity (org.apache.http.HttpEntity)55 BasicHttpParams (org.apache.http.params.BasicHttpParams)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)48 InputStream (java.io.InputStream)47 HttpParams (org.apache.http.params.HttpParams)43 Scheme (org.apache.http.conn.scheme.Scheme)39 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)38 ArrayList (java.util.ArrayList)32 URI (java.net.URI)30 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)30 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)29 InputStreamReader (java.io.InputStreamReader)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)27