我正在尝试通过DefaultHttpClient对我的本地WebDav服务器进行身份验证,但无论我做什么,它都会返回true。如果密码错误,我想得到假,如果密码正确,我想得到。我该怎么办?
以下是我使用的内容:
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionReuseStrategy();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, 8080),
new UsernamePasswordCredentials(
"admin", "asdasdasd")); // dummy password
HttpPost httppost = new HttpPost(
"http://192.168.99.1:8080/list/");
HttpResponse response = httpclient.execute(httppost); // Execute
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an
// InputStream
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
sb.append(line + "\n");
}
is.close();
答案 0 :(得分:0)
您是否尝试过获取响应的状态代码?
response.getStatusLine().getStatusCode()
此致
答案 1 :(得分:0)
试试这个家伙,
HttpPost httppost = new HttpPost(your URL)
try {
// Build JSON string
JSONStringer user = new JSONStringer().object().key("user")
.object().key("username")
.value(username).key("password")
.value(password).endObject()
.endObject();
StringEntity entity = new StringEntity(user.toString(), "UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
entity.setContentType("application/json");
httppost.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
//System.out.println(httppost.toString());
HttpResponse response = httpClient.execute(httppost);
HttpEntity rp = response.getEntity();
//String builder from response
if (rp.getContentLength() != 0) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(rp.getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
origresponseText = sb.toString();
}
答案 2 :(得分:0)
好的家伙我已经解决了我的问题。我将发布解决方案并希望这也能帮助其他人。
try
{
String encodedAuth = Base64.encodeToString(
(username + ":" + password).getBytes(), Base64.NO_WRAP);
URL url = new URL(webDavURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestProperty("Authorization", "Basic "
+ encodedAuth);
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
if (code == 200)
{
result = "1";
}
else
{
result = "0";
}
}
catch (Exception e)
{
result = "0";
}