2012-06-19 74 views
0

我想向我的請求添加身份驗證標頭。我使用Apache httpclient 4.0中的DefaultHttpClient。無法使用DefaultHttpClient進行身份驗證

我發現這樣做了這種方式:

URI uri = new URI("http://www.bla.bla/folder/"); 

String host = uri.getHost(); 
int port = uri.getPort(); 

httpClient.getCredentialsProvider().setCredentials(
     new AuthScope(host, port, AuthScope.ANY_SCHEME), 
     new UsernamePasswordCredentials("myuser", "mypassword") 
     ); 

這就是執行,甚至與調試器我看到一些憑證變量HttpClient的設定在做請求的時刻。但我檢查與查爾斯網絡流量,沒有驗證頭。瓦爾的

內容:

主持人:www.bla.bla

口:-1

順便說一句。我啓用了查爾斯作爲代理來查看請求的頭,用:

HttpHost proxy = new HttpHost("127.0.0.1", 8888, "http"); 
httpParameters.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

我認爲不應該改變我的頭,將毫無意義webproxy ......反正如果我禁用代理東東吧也不起作用(雖然我看不到標題的內容,但我想這是相同的原因)。 How can I send HTTP Basic Authentication headers in Android?

我得到完全相同的請求,沒有認證頭:

而且使用請求攔截器像Softhinker.com的帖子這裏所描述嘗試。

我在做什麼錯?

在此先感謝。

+0

無論是解釋和解決方案涵蓋在這個線程... http://stackoverflow.com/questions/18108783/apache-httpclient-doesnt-set-basic-authentication-credentials – 2014-05-07 16:19:29

回答

0

我得到它的工作在請求中設置標題「手動」。

request.setHeader(new BasicHeader("Authorization", authstring)); 
相關問題