getInputStream在Android中的Http摘要身份验证期间提供FileNotFoundException
我正在尝试向具有HTTP摘要身份验证的URL发送POST请求 我正在用它来消化。我可以理解,在这个机制中有两个步骤。在第一步中,客户端发送一个请求,服务器响应一个质询。在第二步中,客户端将质询合并到请求中,并从服务器获取响应。使用上面提到的类,它工作正常 我不得不将HttpConnection更改为HttpURLConnection,以使其在Android中工作 现在,我有了DataConnection类,它使用Digest Auth类来完成工作。课程安排如下:getInputStream在Android中的Http摘要身份验证期间提供FileNotFoundException,android,api,http-digest,Android,Api,Http Digest,我正在尝试向具有HTTP摘要身份验证的URL发送POST请求 我正在用它来消化。我可以理解,在这个机制中有两个步骤。在第一步中,客户端发送一个请求,服务器响应一个质询。在第二步中,客户端将质询合并到请求中,并从服务器获取响应。使用上面提到的类,它工作正常 我不得不将HttpConnection更改为HttpURLConnection,以使其在Android中工作 现在,我有了DataConnection类,它使用Digest Auth类来完成工作。课程安排如下: import java.io.B
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import android.content.ContentResolver;
import android.os.Build;
import android.util.Log;
public class DataConnection {
private static DataConnection instance = null;
DigestAuthHandler dah = new DigestAuthHandler("mytestusername",
"myencryptedtestpassword");
public static DataConnection getInstance() throws IOException {
if (instance == null) {
instance = new DataConnection();
}
return instance;
}
public HttpURLConnection getHttpConnection(String request_url)
throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(request_url)
.openConnection();
conn.setAllowUserInteraction(false);
conn.setInstanceFollowRedirects(true);
conn.setChunkedStreamingMode(0);
conn.setRequestProperty("User-Agent", System.getProperty("http.agent"));
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Accept",
"application/json,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
conn.setRequestProperty("Accept-Charset", "UTF-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Pragma", "no-cache");
conn.setRequestProperty("Cache-Control", "no-cache");
// if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO)
// System.setProperty("http.keepAlive", "false");
return conn;
}
public String sendPost(String request_url, byte[] data) throws Exception {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
String responseData = "";
boolean reconnect = true;
while (reconnect) {
try {
connection = getHttpConnection(request_url);
dah.prepareHeaders(connection,
request_url.substring(request_url.indexOf('/', 8)));
connection.setRequestProperty("Content-Length", data.length
+ "");
connection.setDoOutput(true); // POST Method.
connection.connect();
if (connection != null) {
os = connection.getOutputStream();
os.write(data);
os.flush();
reconnect = dah.processHeaders(connection);
int responseCode = connection.getResponseCode();
String responseMessage = connection.getResponseMessage();
is = connection.getInputStream();
int responseLength = (int) connection.getContentLength();
if (responseLength != -1) {
byte[] incomingData = new byte[responseLength];
is.read(incomingData);
responseData = new String(incomingData);
} else {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
responseData = new String(bytestream.toByteArray());
bytestream.close();
}
}
} catch (Exception exception) {
throw exception;
} finally {
if (connection != null) {
connection.disconnect();
}
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
}
try {
JSONObject responseJson = new JSONObject(responseData);
Integer status = (Integer) responseJson.get("status");
int val = status.intValue();
if (val == 999999) {
Log.e("Error 99999",
"Client application must be upgraded.");
}
} catch (Exception e) {
e.printStackTrace();
}
return responseData;
}
}
import java.io.ByteArrayOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入org.json.JSONObject;
导入android.content.ContentResolver;
导入android.os.Build;
导入android.util.Log;
公共类数据连接{
私有静态数据连接实例=null;
DigestAuthHandler dah=新的DigestAuthHandler(“mytestusername”,
“myencryptedtestpassword”);
公共静态数据连接getInstance()引发IOException{
if(实例==null){
实例=新数据连接();
}
返回实例;
}
公共HttpURLConnection getHttpConnection(字符串请求\u url)
抛出异常{
HttpURLConnection conn=(HttpURLConnection)新URL(请求URL)
.openConnection();
conn.setAllowUserInteraction(假);
conn.setInstanceFollowRedirects(真);
conn.setChunkedStreamingMode(0);
conn.setRequestProperty(“用户代理”,System.getProperty(“http.Agent”);
conn.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded”);
conn.setRequestProperty(“接受”,
“application/json,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5”);
conn.setRequestProperty(“接受字符集”,“UTF-8;q=0.7,*;q=0.7”);
conn.setRequestProperty(“Pragma”,“无缓存”);
conn.setRequestProperty(“缓存控制”、“无缓存”);
//if(Integer.parseInt(Build.VERSION.SDK)
在这里,我在getInputStream()
方法调用期间为URL获取FileNotFoundException
。我已经多次验证了我请求的URL和Digest Auth的凭据是100%正确的
请帮忙
注意。通过检查空白连接解决了上述问题。问题是,连接是空的,但我试图调用connection.getInputStream(),因此它给出了错误。如果您可以发布代码片段来显示您是如何检查空连接的,这对其他用户将很有用。我假设您没有通过
if(con==null)
;)完成此操作你能把你的答案标为“正确答案”吗?