0

HTTP ダイジェスト認証を持つ URL に POST リクエストを送信しようとしています。

これをダイジェストに使用しています。このメカニズムには2つのステップがあることが理解できます。最初のステップでは、クライアントがリクエストを送信し、サーバーがチャレンジで応答します。2 番目のステップでは、クライアントは要求にチャレンジを組み込み、サーバーから応答を取得します。そのことは、上記のクラスを使用して適切に機能しています。

Android で動作させるには、HttpConnection を HttpURLConnection に変更する必要がありました。

これで、Digest Auth クラスを使用して作業を完了する DataConnection クラスができました。クラスは以下の通りです。

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;
    }
}

ここでは、メソッド呼び出しFileNotFoundException中に URL を取得します。getInputStream()要求している URL と Digest Auth の資格情報が 100% 正しいことを何度も確認しました。

助けてください。

よろしく。

4

1 に答える 1