希望你們中的一些人可以幫助我解決我的「大腦僵局」。Android,HttpsUrlConnection:POST參數和密碼接收InputStream
(只是爲了解)我試圖通過使用php腳本接收數據庫。因此,我必須調用一個webadress /服務器,只有當我使用有效的用戶名/ pwd-combo併發送正確的參數時,才能訪問php-script。
我的代碼在使用HttpClient和HttpPost時運行良好。 (下面的好的代碼)
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:[email protected]/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的,而不是上面的解決方案。爲什麼要清楚,實時系統使用HTTPS(服務器認證)。
現在我浪費了幾天試圖找出如何...
- POST參數與HttpURLConnection的
- 身份驗證服務器上
- 避免認證檢查一般(用於測試!)
我只是不知道我犯了什麼錯誤。下面到目前爲止我的代碼...
String urlString = "http://USERNAME:[email protected]/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(),活動即會中止。但getOutputStream()也沒有有效的值。
現在的問題。任何人都可以請查看我的代碼,並告訴我如何以正確的方式參數POST。除此之外,你知道我是否可以通過URL發送認證?特別是HTTP的部分使我感興趣。對於非安全模式,我已經有一個正在運行的解決方案。
感謝您的幫助!
我到目前爲止使用的是... http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests http://stackoverflow.com/questions/5859667/how-to-send-requestparameter-in-post-request-using-httpurlconnection – njsmecho
也這些鏈接... http://stackoverflow.com/questions/995514/https -connection-android http://home-1.worldonline.nl/~bmc88/java/sbook/045.html http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post- data-to-web-server/139 http://digitallibraryworld.com/?p=189 – njsmecho