因此,我們知道如何執行http獲取和發佈連接。 http://exampledepot.com/egs/java.net/pkg.html 我們希望將憑據(uname,passwd)傳遞給任何Web服務器以訪問url或獲取響應。 而且我們不能將它作爲後期參數傳遞。 所以看看@這個非常簡單的代碼就可以做到這一切。通過HTTP認證將憑據傳遞給服務器並獲得響應的完整指南
4
A
回答
3
try
{
// Creatin the connection
URL url = new URL("http://yoururl");
URLConnection conn = url.openConnection();
//sendin the base64 encoded credentials thru d header
conn.setRequestProperty(
"Authorization",
"Basic "+ BasicAuth.encode(username, password));
// Get the response
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
//readin d response till d end
while ((line = rd.readLine()) != null) {
// Process line...
Log.v("", line);
}
rd.close();
}
catch (Exception e) { }
爲了對憑證進行編碼,我使用了一個名爲「BasicAuth.java」的簡單外部類,您可以將它添加到您的項目中。 BasicAuth.java
public class BasicAuth {
private BasicAuth() {}
// conversion table
private static byte[] cvtTable = {
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E',
(byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J',
(byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O',
(byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T',
(byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y',
(byte)'Z',
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e',
(byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j',
(byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o',
(byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t',
(byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y',
(byte)'z',
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4',
(byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9',
(byte)'+', (byte)'/'
};
/**
* Encode a name/password pair appropriate to
* use in an HTTP header for Basic Authentication.
* name the user's name
* passwd the user's password
* returns String the base64 encoded name:password
*/
static String encode(String name,
String passwd) {
byte input[] = (name + ":" + passwd).getBytes();
byte[] output = new byte[((input.length/3) + 1) * 4];
int ridx = 0;
int chunk = 0;
/**
* Loop through input with 3-byte stride. For
* each 'chunk' of 3-bytes, create a 24-bit
* value, then extract four 6-bit indices.
* Use these indices to extract the base-64
* encoding for this 6-bit 'character'
*/
for (int i = 0; i < input.length; i += 3) {
int left = input.length - i;
// have at least three bytes of data left
if (left > 2) {
chunk = (input[i] << 16)|
(input[i + 1] << 8) |
input[i + 2];
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = cvtTable[(chunk&0x3F)];
} else if (left == 2) {
// down to 2 bytes. pad with 1 '='
chunk = (input[i] << 16) |
(input[i + 1] << 8);
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = cvtTable[(chunk&0xFC0) >> 6];
output[ridx++] = '=';
} else {
// down to 1 byte. pad with 2 '='
chunk = input[i] << 16;
output[ridx++] = cvtTable[(chunk&0xFC0000)>>18];
output[ridx++] = cvtTable[(chunk&0x3F000) >>12];
output[ridx++] = '=';
output[ridx++] = '=';
}
}
return new String(output);
}
}
+0
看起來不錯,我如何用HttpClient做到這一點? http://developer.android.com/intl/de/reference/org/apache/http/client/HttpClient.html – Janusz 2010-03-08 14:15:28
相關問題
- 1. Xamarin iOS:通過http傳遞憑證
- 2. 將數據傳遞給另一個表單並獲得響應
- 3. 將憑證傳遞給WebProxy?
- 4. 將當前用戶憑據傳遞給遠程服務器
- 5. 通過Gradle傳遞數據庫憑證
- 6. 將憑據通過Google App Engine傳遞給Google閱讀器
- 7. 如何通過'webservice憑證'傳遞給我的web服務端點?
- 8. 將憑證傳遞給Glassfish管理員Web服務
- 9. C#網絡憑證沒有被傳遞給服務器?
- 10. 如何在ping服務器之後獲得完整響應並將其分配給Python中的變量?
- 11. 向服務器發送HTTP POST請求並獲得響應?
- 12. 如何將數據庫憑據傳遞給wcf服務?
- 13. 通過HTTP響應傳遞UN和PW
- 14. 如何通過Android應用程序將登錄憑據傳遞給asp服務器
- 15. angular 2如何將服務響應數據傳遞給UI
- 16. 將SQL登錄憑證傳遞給Delphi Datasnap服務器的最有效方法?
- 17. 將指南針點傳遞給PHP
- 18. 將Windows憑據傳遞給遠程https WCF服務
- 19. 將Windows用戶憑據傳遞給Web服務
- 20. 將用戶憑據從aspnet站點傳遞給wcf服務
- 21. 通過GET請求和響應將對象傳遞給WCF服務
- 22. 將AWS憑證傳遞給PigStorage函數
- 23. 如何將憑據傳遞給WCF服務以進行Windows身份驗證Silverlight
- 24. 如何通過Angular 4獲得完整的錯誤響應?
- 25. http服務器認證
- 26. 通過Shiny服務器將閃亮輸入傳遞給R markdown
- 27. 通過iframe傳遞登錄憑證
- 28. 通過flex4傳遞網絡憑證
- 29. 如何通過DalSoft.RestClient傳遞憑證?
- 30. hg推HTTP服務器憑證
而問題是......什麼? – CommonsWare 2009-12-28 13:45:18
沒問題。只是爲了幫助其他用戶。 我想我應該把代碼添加到答案部分。 – Bohemian 2009-12-28 13:51:24