tôi sử dụng Apache HttpComponents để truy cập một dịch vụ web, và don' biết làm thế nào để thiết lập người dùng/mật khẩu trong yêu cầu, đây là mã của tôi:làm thế nào để thiết lập tên người dùng/mật khẩu trong httpget
URI url = new URI(query);
HttpGet httpget = new HttpGet(url);
DefaultHttpClient httpclient = new DefaultHttpClient();
Credentials defaultcreds = new UsernamePasswordCredentials("test", "test");
httpclient.getCredentialsProvider().setCredentials(new AuthScope(HOST, AuthScope.ANY_PORT), defaultcreds);
HttpResponse response = httpclient.execute(httpget);
..
nhưng vẫn nhận được lỗi trái phép 401.
HTTP/1.1 401 Unauthorized [Server: Apache-Coyote/1.1, Pragma: No-cache, Cache-Control: no-cache, Expires: Wed, 31 Dec 1969 16:00:00 PST, WWW-Authenticate: Basic realm="MemoryRealm", Content-Type: text/html;charset=utf-8, Content-Length: 954, Date: Wed, 04 Apr 2012 02:28:49 GMT]
Tôi không chắc chắn nếu đúng cách để đặt người dùng/mật khẩu? bất cứ ai có thể giúp đỡ? cảm ơn.