- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我遇到很多抢先式身份验证问题之后,我终于让它工作了。现在下一个问题。我想用它获取一个文件,但我不知道如何获取。我认为文件数据可能在变量响应中,但事实并非如此。任何想法这可能如何工作?我尝试了好几天都没有成功 :( - 基本上我正在尝试下载一个 jpeg 文件,该文件位于受 prem.auth 保护的服务器上。
// BASIC AUTH
/*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
//http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(username, password));
// Generate BASIC scheme object and stick it to the local
// execution context
BasicHttpContext localcontext = new BasicHttpContext();
BasicScheme basicAuth = new BasicScheme();
localcontext.setAttribute("preemptive-auth", basicAuth);
//first request interceptor
httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);
HttpHost targetHost = new HttpHost(host, port, "http");
//HttpGet httpget = new HttpGet("/");
HttpGet httpget = new HttpGet(http.url);
System.out.println("executing request" + httpget.getRequestLine());
/// !!!
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println("+"+response.getStatusLine()+"+");
...
最佳答案
好吧,我现在自己解决了 - 这里是解决方案:
StringBuffer tmp = new StringBuffer(); // for content
if (entity != null) {
InputStream is = entity.getContent();
int l = 0;
byte[] t = new byte[1024];
while ((l = is.read(t)) != -1) {
for (int i=0; i<l ; i++){
tmp.append((char)t[i]);
}
}
}
return tmp.toString();
关于android - 使用 DefaultHTTPClient 和抢先身份验证下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011770/
我尝试在 Android Java 环境中检索对 URL 的 POST 响应: 这是我的代码: try{ DefaultHttpClient
我正在使用 DefaultHttpClient 的 singleton 实例与 PoolingClientConnectionManager 一起执行大量(每秒多次)HTTP 操作以多线程的方式。我的
我有一段使用 DefaultHttpClient 的代码,它发出登录网站的发布请求。 但是随后的请求似乎忘记了我已登录的事实。 如何打开我正在使用的 DefaultHttpClient 的自动 coo
我想将 DefaultHttpClient 转换为单例类 public class HttpClient { private static final String TAG = "&&----HT
我有一个 Apache 提供的 DefaultHttpClient,我创建并执行不同的请求。 问题是与服务器的连接可能会建立相当长的时间,并且我的程序的用户可能想要取消此过程。 是否有任何有效的方法可
我对 DefaultHttpClient 中的超时如何工作感到有点困惑。 我正在使用这段代码: private DefaultHttpClient createHttpClient() {
我在我当前的应用程序中使用 DefaultHttpClient。 我读过这篇文章,其中指出 DefaultHttpClient 已弃用:http://developer.android.com/ref
我想做什么 大家好,我正在尝试创建一个应用程序,我可以在其中查看客户给我的订单。为此,我在我的服务器上创建了一个接口(interface),我可以在该接口(interface)上发送 post/get
我的问题是,如果我没有指定,使用 DefaultHttpClient 发出的请求的默认超时是多少。 如果没有这样的代码 HttpParams my_httpParams = new BasicHttp
我正在尝试使用 Android 的 DefaultHTTPClient 获取页面并使用 Jsoup 解析它。我收到一个非常奇怪的响应,其中 中的所有 HTML和标签被编码成某种东西。
我想将带有两个参数的 POST 请求从 JsonObject 发送到 python API,该 API 响应 True 或 False,但它根本不起作用... 我的代码: // Create a ne
我正在使用 DefaultHttpClient 将数据发送到网络...使用以下代码块发送数据: nameValuePairs.add(new BasicNameValuePair(name, valu
这是我的 AsyncTask 类: public class JSONParser extends AsyncTask { public interface MyCallbackInterfac
我是 servlet 应用程序的新手,正在尝试学习该主题。在途中,我编写了一个名为 FormWebServlet 的 servlet 类,它使用 org.apache.http.impl.client
尝试了几乎所有方法,但没有成功使 DefaultHttpClient() 使用过期的 SSL 证书。有没有人有一些例子或可以提供帮助的东西。 Android SDK 8 最佳答案 您可以使用自定义 T
我遇到了 Android 上 DefaultHttpClient 的超时问题。我正在尝试使用以下代码设置超时: HttpClient client = new DefaultHttpClient();
在我遇到很多抢先式身份验证问题之后,我终于让它工作了。现在下一个问题。我想用它获取一个文件,但我不知道如何获取。我认为文件数据可能在变量响应中,但事实并非如此。任何想法这可能如何工作?我尝试了好几天都
我如何将字符串数据 (JSONObject.toString()) 发送到 url。我想在 util 类中编写一个静态方法来执行此操作。我希望方法签名如下 public static String p
我正在做一个项目,我需要调用我的服务,我的服务将以 JSON 格式返回数据。而且我不需要将这个 JSON 响应序列化到任何 POJO,我只需要将数据作为 String 取回。这个应用程序对性能非常关键
在我的代码中有这个 Log.d("WFlog (executeRequest)", request.toString()) ; httpResponse = clien
我是一名优秀的程序员,十分优秀!