希望你们中的一些人可以帮助我解决我的“大脑僵局”。
(只是为了理解)我正在尝试使用 php 脚本接收数据库。因此,我必须调用一个网络地址/服务器,只有当我使用有效的用户名/密码组合并发送正确的参数时,我才能访问 php 脚本。
使用 HttpClient 和 HttpPost 时,我的代码运行良好。(在 GOOD 代码下方)
    InputStream myInputDB = null;
    OutputStream myOutputDB = null;
    HttpResponse response = null;
    // Path to the just created empty db
    String dbFilePath = DB_PATH + KeyConstants.DB_NAME;
    try {
        // Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("version", "20110516140000"));
        HttpPost httppost = new HttpPost("http://USERNAME:PASSWORD@SomeAdress/u/db2.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = httpClient.execute(httppost);
        //Open your local db as the input stream
        myInputDB = response.getEntity().getContent();
        //Open the empty db as the output stream
        myOutputDB = new FileOutputStream(dbFilePath);
        //transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
        int length;
        while ((length = myInputDB.read(buffer)) > 0) {
            myOutputDB.write(buffer, 0, length);
        }
        //Close the streams
        myOutputDB.flush();
        myOutputDB.close();
        myInputDB.close();
现在我正在尝试使用 HttpURLConnection/HttpsURLConnection 而不是上述解决方案。为什么要清楚,live-system使用HTTPS(服务器认证)。
到现在为止,我浪费了几天试图弄清楚如何...
- 带有 HttpURLConnection 的 POST 参数
- 在服务器上进行身份验证
- 一般避免认证检查(用于测试!)
我只是不知道我在哪里犯了错误。到目前为止,在我的代码下方...
    String urlString = "http://USERNAME:PASSWORD@SomeAdress/u/db2.php";
    String params = "version=20110516140000";
    InputStream stream = null;
    url = new URL(urlString);
    if (url.getProtocol().toLowerCase().equals("https")) {
        trustAllHosts();
        HttpsURLConnection urlconn = (HttpsURLConnection) url.openConnection();
        urlconn.setHostnameVerifier(DO_NOT_VERIFY);
        urlconn.setDoInput(true);
        urlconn.setDoOutput(true);
        urlconn.setRequestMethod("POST");
        urlconn.setRequestProperty("Content-Type", "text/xml");
        urlconn.setRequestProperty("Content-Length", String.valueOf(params.getBytes().length));
        OutputStreamWriter wr = new OutputStreamWriter(urlconn.getOutputStream());
        wr.write(params);
        wr.flush();
        stream = urlconn.getInputStream();
        if ("gzip".equals(urlconn.getContentEncoding())) {
            stream = new GZIPInputStream(stream);
        }
        wr.close();
    } else {            
        // Almost the same as if-path, but without trustAllHosts() and with HttpURLConnection instead of HttpsURLConnection         
    }
    return stream;
我需要的是一个 InputStream,然后我可以在 android 手机上保存/复制它(如在工作代码中)。现在我得到的结果是一个 IOException。调用 urlconn.getInputStream() 后,Activity 立即中止。但是 getOutputStream() 也没有有效值。
现在的问题。任何人都可以查看我的代码并告诉我如何以正确的方式发布参数。除此之外,您知道我是否可以通过 URL 发送身份验证吗?特别是 HTTP'S' 部分让我很感兴趣。对于非安全模式,我已经有一个正在运行的解决方案。
谢谢你的帮助!