Search in sources :

Example 36 with DefaultHttpClient

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

the class ConfigureBaseUriIT method shouldUseRequestUriWhenNoXForwardHeadersPresent.

@Test
public void shouldUseRequestUriWhenNoXForwardHeadersPresent() throws Exception {
    URI rootUri = functionalTestHelper.baseUri();
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(rootUri);
        httpget.setHeader("Accept", "application/json");
        HttpResponse response = httpclient.execute(httpget);
        String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
        byte[] data = new byte[Integer.valueOf(length)];
        response.getEntity().getContent().read(data);
        String responseEntityBody = new String(data);
        assertFalse(responseEntityBody.contains("https://foobar.com"));
        assertFalse(responseEntityBody.contains(":0"));
        assertTrue(responseEntityBody.contains("http://localhost"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Also used : DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Test(org.junit.Test)

Example 37 with DefaultHttpClient

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

the class HttpUtils_Deprecated method loadImageFromUrl.

public static byte[] loadImageFromUrl(String url) {
    InputStream i = null;
    byte[] filename = null;
    try {
        byte[] dbfilename = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String geturl = url;
        HttpGet request = new HttpGet(geturl);
        request.setHeader("referer", "http://pic.qingdaonews.com");
        HttpResponse response = httpClient.execute(request);
        i = response.getEntity().getContent();
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = i.read()) != -1) {
            bytestream.write(ch);
        }
        filename = bytestream.toByteArray();
        bytestream.close();
        i.close();
        return filename;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return filename;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 38 with DefaultHttpClient

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

the class HttpUtils_Deprecated method GetJson_Cookie.

/**
     * 处理httpResponse信息,返回json,保存cookie
     *
     * @param
     * @return String
     */
public static String GetJson_Cookie(String httpUrl) {
    String strResult = null;
    try {
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(httpUrl);
        // 取得HttpClient对象
        HttpClient httpclient = new DefaultHttpClient();
        // 请求HttpClient,取得HttpResponse
        HttpResponse httpResponse = httpclient.execute(httpRequest);
        //保存cookie
        StringBuffer sb = new StringBuffer();
        String inputLine = "";
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            Header[] hds = httpResponse.getAllHeaders();
            int isok = 0;
            for (int index = 0; index < hds.length; index++) {
                if ("Set-Cookie".equals(hds[index].getName())) {
                    String value = hds[index].getValue();
                    String[] vs = value.split(";");
                    for (int i = 0; i < vs.length; i++) {
                        String[] vss = vs[i].split("=");
                        if ("member".equals(vss[0])) {
                            rst = vs[i] + ";";
                            Log.d("Chen", "cookie信息:" + rst);
                            isok++;
                        }
                    }
                }
            }
        }
        // 请求成功
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // 取得返回的字符串
            strResult = EntityUtils.toString(httpResponse.getEntity());
        } else {
            Log.i(TAG, "请求错误");
        }
    } catch (Exception e) {
        Log.i(TAG, "请求网络异常");
        strResult = "net_ex";
    }
    return strResult;
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) MalformedURLException(java.net.MalformedURLException)

Example 39 with DefaultHttpClient

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

the class HttpUtils_Deprecated method getResponseFromGetUrl.

/*
     * 发送GET请求,通过URL和参数获取服务器反馈应答,通过返回的应答对象 在对数据头进行分析(如获得报文头 报文体等) params 的格式为
     * key1=value1,key2=value2 url 是一个不带参数的URL params是发送get请求的参数 其将直接在url后面添加
     * headinfo 是发送get请求的微博验证信息 即登陆时系统给出的合法用户验证信息
     */
public static String getResponseFromGetUrl(String url, String logininfo, String params) throws Exception {
    Log.d("Chen", "url--" + url);
    if (null != params && !"".equals(params)) {
        url = url + "?";
        String[] paramarray = params.split(",");
        for (int index = 0; null != paramarray && index < paramarray.length; index++) {
            if (index == 0) {
                url = url + paramarray[index];
            } else {
                url = url + "&" + paramarray[index];
            }
        }
    }
    HttpGet httpRequest = new HttpGet(url);
    httpRequest.addHeader("Cookie", logininfo);
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 30000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 30000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
    // DefaultHttpClient httpclient = new DefaultHttpClient();
    StringBuffer sb = new StringBuffer();
    try {
        HttpResponse httpResponse = httpclient.execute(httpRequest);
        String inputLine = "";
        // Log.d("Chen","httpResponse.getStatusLine().getStatusCode()"+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();
        } else if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
            return "not_modify";
        }
    } catch (Exception e) {
        e.printStackTrace();
        return "net_error";
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return sb.toString();
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) MalformedURLException(java.net.MalformedURLException)

Example 40 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)

Aggregations

DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)481 HttpResponse (org.apache.http.HttpResponse)293 HttpGet (org.apache.http.client.methods.HttpGet)227 HttpClient (org.apache.http.client.HttpClient)203 IOException (java.io.IOException)158 HttpPost (org.apache.http.client.methods.HttpPost)107 Test (org.junit.Test)86 HttpEntity (org.apache.http.HttpEntity)84 ClientProtocolException (org.apache.http.client.ClientProtocolException)64 InputStream (java.io.InputStream)60 Scheme (org.apache.http.conn.scheme.Scheme)53 BasicHttpParams (org.apache.http.params.BasicHttpParams)51 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)49 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)48 HttpParams (org.apache.http.params.HttpParams)47 ArrayList (java.util.ArrayList)45 URI (java.net.URI)43 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)38 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)38 InputStreamReader (java.io.InputStreamReader)37