Note that there are some explanatory texts on larger screens.

plurals
  1. POgetInputStream gives FileNotFoundException during Http Digest Authentication in Android
    primarykey
    data
    text
    <p>I am trying to send a POST request to a URL that has HTTP Digest authentication.</p> <p>I am using <a href="http://read.pudn.com/downloads73/sourcecode/mobile/j2me/13232/HttpClient/src/com/enterprisej2me/HttpConnections/DigestAuthHandler.java__.htm" rel="nofollow">this</a> for the Digest. I can understand that in this mechanism there are 2 steps. In the first step the client sends a request and the server responses with a challenge. In the second step the client incorporates the challenge in the request and gets the response from the server. That thing is working properly using the above mentioned class.</p> <p>I had to change HttpConnection to HttpURLConnection to make it work in Android.</p> <p>Now, I have the DataConnection class which uses the Digest Auth class to get the work done. The class is as below:</p> <pre><code>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) &lt; 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; } } </code></pre> <p>Here, I get <code>FileNotFoundException</code> for the URL during <code>getInputStream()</code> method call. I have verified many times that the URL I'm requesting and the credentials for Digest Auth is 100% correct.</p> <p>Please Help.</p> <p>Regards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload