标签: apache-commons-httpclient

我需要Android中的HttpClient替代选项,以便将数据发送到PHP,因为它不再受支持

目前我使用HttpClient,HttpPost将数据发送到我的PHP serverAndroid app,但所有这些方法是在API 22弃用,在API 23取出,那么有什么替代方案呢?

我到处搜索,但没有找到任何东西.

java api android http-post apache-commons-httpclient

27
推荐指数
3
解决办法
5万
查看次数

DefaultHttpClient到AndroidHttpClient

我的代码有问题,我希望得到一些帮助.我是第一次使用这段代码:

        new DefaultHttpClient().execute(new HttpGet(linkk)).getEntity().writeTo(
           new FileOutputStream(f));
Run Code Online (Sandbox Code Playgroud)

它在Android 2.3上运行得很好但在4.0上却没有.经过一些研究,我听说使用AndroidHttpClient更好,这样它就可以在4.0和3.1上运行.问题是我不知道我是否正确修改了我的代码,而且互联网上没有太多关于AndroidhttpClient的例子.

这是我调整后的代码:

    AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    HttpGet request = new HttpGet(linkk);   
    HttpResponse response = client.execute(request); //here is where the exception is thrown    
    response.getEntity().writeTo(new FileOutputStream(f));
Run Code Online (Sandbox Code Playgroud)

这是logcat显示的内容:

     01-03 01:32:11.950: W/dalvikvm(17991): threadid=1: thread exiting with uncaught exception (group=0x40a2e1f8)
     01-03 01:32:11.986: E/AndroidRuntime(17991): FATAL EXCEPTION: main
     01-03 01:32:11.986: E/AndroidRuntime(17991): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lacra.fbirthdays/com.lacra.fbirthdays.ListV}: android.os.NetworkOnMainThreadException
     01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
     01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
     01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
     01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
     01-03 01:32:11.986: …
Run Code Online (Sandbox Code Playgroud)

android apache-commons-httpclient

26
推荐指数
2
解决办法
5万
查看次数

HttpClient没有任何异常

我正在开发一个长期运行的应用程序,该应用程序大量使用来自apache的HttpClient.

在我的第一次测试运行中,应用程序运行良好,直到它被卡住.它没有停止,它没有抛出任何异常,只是坐在那里什么都不做.

我刚刚做了第二次跑步并停止了时间,并在大约停止后停止了.24小时不间断运行.此外,我注意到我运行它的笔记本电脑的互联网连接在应用程序卡住的确切时刻终止.我不得不重启我的WLAN适配器,以便再次运行网络.

但是,应用程序在连接再次启动后没有返回工作状态.现在,它又被卡住了.

在HttpClient中是否有任何超时控制器我都不知道?为什么我的应用程序在连接断开时不会抛出异常?

使用客户端的部分如下所示;

public HttpUtil(ConfigUtil config) {
    this.config = config;

    client = new DefaultHttpClient();
    client.getParams().setParameter(HttpProtocolParams.USER_AGENT, this.config.getProperty("httputil.userAgent"));
}

public String getContentAsString(String url) throws ParseException, ClientProtocolException, IOException {
    return EntityUtils.toString(
            client.execute(
                    new HttpGet(url)).getEntity());
}
Run Code Online (Sandbox Code Playgroud)

应用程序反复调用httputil.getContentAsString()所需的URL.

java apache-commons-httpclient

25
推荐指数
4
解决办法
3万
查看次数

Android会话管理

是否有特定的Android会话管理库?我需要在普通的Android应用中管理我的会话.不在WebView.我可以从post方法设置会话.但是当我发送另一个会话失败的请求时.有人可以帮我解决这个问题吗?

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("My url");

HttpResponse response = httpClient.execute(httppost);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();

if (cookies.isEmpty()) {
    System.out.println("None");
} else {
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试访问会话丢失的同一主机时:

HttpGet httpGet = new HttpGet("my url 2");
HttpResponse response = httpClient.execute(httpGet);
Run Code Online (Sandbox Code Playgroud)

我得到了登录页面响应正文.

java session android session-cookies apache-commons-httpclient

24
推荐指数
1
解决办法
3万
查看次数

从HttpClient 3.x迁移到4.x.

我们使用commons-httpclient-3.1编写了一些需要转换为4.1的代码.我对此有点熟悉,现在正在阅读4.1 httpclient教程.

我在这里看到了几个帖子(例如从HttpClient 3转换为4但这是关于特定的构造).似乎应该有一些文档/示例显示如何将3.x的使用升级到4.x?

一个具体的例子:替换org.apache.commons.httpclient.HttpState的使用

java apache-commons-httpclient apache-httpclient-4.x

22
推荐指数
1
解决办法
2万
查看次数

如何强制Commons HTTPClient 3.1仅对HTTPS使用TLS 1.2?

我希望强制Apache Commons HTTP-Client(版本3.1)使用TLS 1.2作为HTTPS 的唯一协议.

这是因为服务器应该升级到TLS 1.2并且不再接受任何旧协议(导致"连接重置"被返回).

对于进一步的上下文,可能是不相关的,HTTP-Client与Axis2一起用于制作SOAP; 用于设置HttpClient的一些代码如下:

MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
this.httpClient = new HttpClient(connMgr);

// initialize HttpClient parameters
HttpClientParams hcParams = this.httpClient.getParams();

// Maximum time to wait to receive connection from pool
hcParams.setConnectionManagerTimeout(this.maxWait);
hcParams.setSoTimeout(this.timeout);
hcParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(this.retryCount, false));

// Initialize global Connection manager parameters
HttpConnectionManagerParams cmParams = connMgr.getParams();
cmParams.setDefaultMaxConnectionsPerHost(this.maxActive);
cmParams.setStaleCheckingEnabled(this.checkStaleConnections);
cmParams.setConnectionTimeout(this.timeout);
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助!

java ssl https apache-commons-httpclient

22
推荐指数
1
解决办法
4万
查看次数

使用Apache HTTPClient - 如何在发送之前看到原始请求字符串?

出于调试目的,我希望看到将要发送的原始请求.有没有办法在没有HTTP监视器的情况下直接从API HttpPostHttpClient

我发现了一些"几乎"重复的问题,但不适用于这个问题

apache-commons-httpclient

19
推荐指数
1
解决办法
3万
查看次数

如何创建CloseableHttpResponse对象来帮助测试?

我正在尝试构建一个CloseableHttpResponse模拟对象,以便在我的一个单元测试中返回,但是它没有构造函数.我找到了这个DefaultHttpResponseFactory,但它只生成一个HttpResponse.构建CloseableHttpResponse的简单方法是什么?我是否需要调用execute()我的测试然后设置statusLineentity?这似乎是一种奇怪的方法.

这是我试图模拟的方法:

public static CloseableHttpResponse getViaProxy(String url, String ip, int port, String username,
                                                String password) {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(ip, port),
            new UsernamePasswordCredentials(username, password));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
    try {
        RequestConfig config = RequestConfig.custom()
                .setProxy(new HttpHost(ip, port))
                .build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(config);

        LOGGER.info("executing request: " + httpGet.getRequestLine() + " via proxy ip: " + ip + " port: " + port +
                " …
Run Code Online (Sandbox Code Playgroud)

java mocking mockito apache-commons-httpclient

17
推荐指数
2
解决办法
2万
查看次数

使用Apache的HTTP客户端时,将HTTP响应作为字符串获取的建议方法是什么?

我刚刚开始使用Apache的HTTP客户端库,并注意到没有内置的方法将HTTP响应作为String获取.我只是想像String那样得到它,以便我可以将它传递给我正在使用的任何解析库.

将HTTP响应作为String获取的推荐方法是什么?这是我提出请求的代码:

public String doGet(String strUrl, List<NameValuePair> lstParams) {

    String strResponse = null;

    try {

        HttpGet htpGet = new HttpGet(strUrl);
        htpGet.setEntity(new UrlEncodedFormEntity(lstParams));

        DefaultHttpClient dhcClient = new DefaultHttpClient();

        PersistentCookieStore pscStore = new PersistentCookieStore(this);
        dhcClient.setCookieStore(pscStore);

        HttpResponse resResponse = dhcClient.execute(htpGet);
        //strResponse = getResponse(resResponse);

    } catch (ClientProtocolException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }

    return strResponse;

}
Run Code Online (Sandbox Code Playgroud)

java apache-commons-httpclient

16
推荐指数
2
解决办法
4万
查看次数

Apache HttpClient摘要式身份验证

基本上我需要做的是执行摘要式身份验证.我尝试的第一件事是这里提供的官方示例.但是当我尝试执行它时(通过一些小的更改,Post而不是Get方法)我得到了一个

org.apache.http.auth.MalformedChallengeException: missing nonce in challange
at org.apache.http.impl.auth.DigestScheme.processChallenge(DigestScheme.java:132)
Run Code Online (Sandbox Code Playgroud)

当这次失败时我尝试使用:

DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), new UsernamePasswordCredentials("<username>", "<password>"));

HttpPost post = new HttpPost(URI.create("http://<someaddress>"));
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("domain", "<username>"));
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

DigestScheme digestAuth = new DigestScheme();
digestAuth.overrideParamter("algorithm", "MD5");
digestAuth.overrideParamter("realm", "http://<someaddress>");
digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
digestAuth.overrideParamter("qop", "auth");
digestAuth.overrideParamter("nc", "0");
digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());

Header auth = digestAuth.authenticate(new
      UsernamePasswordCredentials("<username>", "<password>"), post);
System.out.println(auth.getName());
System.out.println(auth.getValue());
post.setHeader(auth);


HttpResponse ret = client.execute(post);
ByteArrayOutputStream v2 = new ByteArrayOutputStream();
ret.getEntity().writeTo(v2);
System.out.println("----------------------------------------");
System.out.println(v2.toString()); …
Run Code Online (Sandbox Code Playgroud)

java android digest-authentication apache-commons-httpclient

14
推荐指数
2
解决办法
2万
查看次数