我试图通过Android HttpURLConnection
(从中导入org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection
)发送GET ,并在收到响应时抛出IOException:
在doRequestInternal()中:"收到的身份验证质询为空"
这个错误意味着什么,是什么导致了这个?我正在向授权标题写OAuth参数,但我也在其他场合这样做,没有问题.
if (connection == null) {
connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
connection.setRequestMethod("GET");
}
//... do some OAuth message signing
connection.connect();
int statusCode = connection.getResponseCode(); // throws IOException
Run Code Online (Sandbox Code Playgroud) 不幸的是,Android上有很多cookie管理器.Cookie HttpURLConnection
由以下人员维护,java.net.CookieManager
Cookie WebView
由以下人员维护android.webkit.CookieManager
.这些cookie存储库是独立的,需要手动同步.
我的应用程序使用HttpURLConnections
和显示WebViews
(它是本机HTML混合).当然,我希望两者共享所有cookie - 所以我将在整个过程中进行透明会话.
进一步来说:
简单地说 - 我正在寻找双向同步.或者甚至更好,让它们都使用相同的cookie存储库.您可以假设它们同时处于活动状态(如在不同的选项卡上).
问题:
有没有办法让两个使用相同的cookie存储库?
如果没有,建议的做法是手动同步?什么时候我应该同步,怎么样?
相关问题:这个问题解决了类似的问题,但只实现了单向同步(HttpURLConnection - > WebView).
我最好的想法:我真的想避免手动同步,所以我试着想如何让两者都使用相同的存储库.也许我可以创建自己的扩展核心处理程序java.net.CookieManager
.我将使用它将其设置为核心cookie处理程序java.net.CookieHandler.setDefault()
.它的实现将是android.webkit.CookieManager
处理程序实例的代理(对于我只是访问webkit管理器的每个函数).
我正在尝试编写一个应用程序从url下载pdf,将它们存储在sd上,然后由adobe pdf reader或其他能够打开pdf的应用程序打开.
直到现在,我已经"成功下载并将其存储在Sd卡上"(但每当我尝试使用pdf阅读器打开pdf时,阅读器崩溃并发出意外错误),例如,http://maven.apache.组织/ Maven的1.x中/ maven.pdf
这是我的下载程序的代码:
//........code set ui stuff
//........code set ui stuff
new DownloadFile().execute(fileUrl, fileName);
private class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);
return null;
}
}
public class FileDownloader { …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个Java客户端,该客户端POST到需要身份验证的HTTP服务器.
我必须至少支持以下三种身份验证方法:Basic,Digest或Negotiate.此外,POST可能非常大(超过2MB),因此我需要使用流式传输.正如HttpURLConnection所记录的那样
启用输出流时,无法自动处理身份验证和重定向.如果需要身份验证或重定向,则在读取响应时将抛出HttpRetryException.
所以,我需要自己处理身份验证.我搜索并再次搜索了一种使用已经编码的类的方法 - 但是没有办法......
我可以从这里获取所需的源(因为它们是带有Classpath异常的GPLv2).这是正确的方法吗?
谢谢.
随着OkHttp
图书馆,应用程序面临以下SocketTimeoutException
问题.如果请求大小较小,那么它工作正常(小于1MB).我在10秒内收到此异常,即使我的socket timeout(readTimeout
)值更高.它总是失败的请求(大小是1.8MB).当我执行请求时HttpUrlConnection
它工作正常.什么可能是失败的原因?
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: java.net.SocketTimeoutException: timeout
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.Okio$3.newTimeoutException(Okio.java:207)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.AsyncTimeout.exit(AsyncTimeout.java:261)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.AsyncTimeout$1.write(AsyncTimeout.java:158)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.RealBufferedSink.write(RealBufferedSink.java:46)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okhttp3.internal.http.Http1xStream$FixedLengthSink.write(Http1xStream.java:286)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okio.RealBufferedSink.write(RealBufferedSink.java:96)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okhttp3.RequestBody$2.writeTo(RequestBody.java:96)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:704)
03-29 12:16:38.997 32066-4018/com.mobile W/System.err: …
Run Code Online (Sandbox Code Playgroud) 我知道这个问题应该用System.setProperty修复("http.keepAlive","false"); 在openConnection之前,但这对我不起作用.首先尝试这个代码工作,第二个失败.即使我在不到5秒钟后尝试此请求,它也可以.如果我等待更多,它会再次失败
这是我的代码:
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout (30000) ;
conn.setDoOutput(true);
conn.setDoInput(true);
consumer.sign(conn);
InputSource is = new InputSource(conn.getInputStream());
Run Code Online (Sandbox Code Playgroud)
我在最后一行得到了例外:
java.io.IOException: Write error: I/O error during system call, Broken pipe
W/System.err( 2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativewrite(Native Method)
W/System.err( 2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.access$600(OpenSSLSocketImpl.java:55)
W/System.err( 2164): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:583)
W/System.err( 2164): at java.io.OutputStream.write(OutputStream.java:82)
W/System.err( 2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1332)
W/System.err( 2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
W/System.err( 2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
W/System.err( 2164): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1153)
W/System.err( 2164): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:253)
Run Code Online (Sandbox Code Playgroud)
有人知道这里有什么问题吗?谢谢!
我试着写出URLConnection#getOutputStream
,然而,在我打电话之前,实际上没有数据被发送URLConnection#getInputStream
.即使我设置URLConnnection#doInput
为false,它仍然不会发送.有人知道为什么吗?API文档中没有任何内容可以描述这一点.
URLConnection上的Java API文档:http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html
Java的读取和编写URLConnection的教程:http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionTest {
private static final String TEST_URL = "http://localhost:3000/test/hitme";
public static void main(String[] args) throws IOException {
URLConnection urlCon = null;
URL url = null;
OutputStreamWriter osw = null;
try {
url = new URL(TEST_URL);
urlCon = url.openConnection();
urlCon.setDoOutput(true);
urlCon.setRequestProperty("Content-Type", "text/plain");
////////////////////////////////////////
// SETTING THIS TO FALSE DOES NOTHING //
////////////////////////////////////////
// urlCon.setDoInput(false);
osw …
Run Code Online (Sandbox Code Playgroud) 我有一个问题是要理解该类中connect()
方法的含义URLConnection
.在下面的代码中,如果我使用该connect()
方法,如果我不使用它,我会得到相同的结果.
为什么(或何时)我需要使用它?
URL u = new URL("http://example.com");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();//with or without it I have the same result
InputStream in = conn.getInputStream();
int b;
while ((b = in.read()) != -1) {
System.out.write(b);
}
Run Code Online (Sandbox Code Playgroud) 我想知道Android上是否存在HttpUrlConnection和POST请求的已知问题.从Android客户端发出POST请求时,我们遇到间歇性的 EOFExceptions.重试相同的请求最终会起作用.这是一个示例堆栈跟踪:
java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
Run Code Online (Sandbox Code Playgroud)
有许多类似的错误报告和帖子到堆栈溢出但我无法理解是否确实存在问题,如果是,那么什么版本的Android会受到影响以及建议的修复/解决方法是什么.
以下是我所指的一些类似报告:
这是一个潜在的Android框架修复
我知道在Froyo之前的连接池中存在中毒连接的问题,但这些问题仅在新的ICS +设备上发生.如果以后的设备出现问题,我会期待某种官方Android文档的问题.
我在Java中编写了一些简单的代码,该方法应该连接到网站并返回BufferedReader.
private BufferedReader getConnection(String url_a) {
URL url;
try {
System.out.println("getting connection");
url = new URL(url_a);
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
urlConnection.addRequestProperty("User-Agent",
"Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924"
+ "Epiphany/1.4.4 (Ubuntu)");
inStream = new InputStreamReader(urlConnection.getInputStream());
return new BufferedReader(inStream);
} catch (Exception ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
当我在我的电脑上使用它时,它工作正常,但当我把.jar文件放在服务器上时,我收到此错误:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
at dataconverter.Reader.getConnection(Reader.java.260)
Run Code Online (Sandbox Code Playgroud)
问题很奇怪,因为每次都不抛出异常,有时候一切正常,程序运行正常.
有没有人有任何想法?