具体错误信息:
401 - 未授权: 由于凭据无效,访问被拒绝。
您无权使用所提供的凭据查看此目录或页面。
访问代码:
Protocol myHTTPS = new Protocol("https",
new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myHTTPS);
// 设置连接参数
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
PostMethod postMethod = new PostMethod(mailServer
+ "/owa/auth/owaauth.dll");
postMethod.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
NameValuePair destination = new NameValuePair("destination",
mailServer + "/owa/");
NameValuePair flag = new NameValuePair("flag", "0");
NameValuePair forcedownlevel = new NameValuePair("forcedownlevel",
"0");
NameValuePair trusted = new NameValuePair("trusted", "0");
NameValuePair username = new NameValuePair("username", user);
NameValuePair password = new NameValuePair("password", pw);
NameValuePair[] FieldArr = new NameValuePair[] { destination, flag,
forcedownlevel, trusted, username, password };
postMethod.addParameters(FieldArr);
// 取邮件数
int statusCode = httpClient.executeMethod(postMethod);
GetMethod redirect = new GetMethod(mailServer + "/owa/");
redirect.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
httpClient.executeMethod(redirect);
String str = redirect.getResponseBodyAsString();
System.out.println(str);
logger.debug(str);
int start = str.indexOf("</a><span class=\"unrd\">");
int end = str.indexOf(")</span></td></tr><tr>");
if (start != -1 && end != -1) {
start += "</a><span class=\"unrd\">".length();
rtnStr = str.substring(start + 1, end);
}
postMethod.releaseConnection();
httpClient = null;
返回值就是上面说的服务器错误信息。
但是我通过网页登陆是正常的,请问大家这是怎么回事!
------解决方案--------------------
不是说你有user/password http client就自动会帮你验证的
你需要用诸如FBA之类的机制进行验证
不过我没有例子了,你自己检索一下FBA, Form based authentication
------解决方案--------------------
你想用程序操作用户的邮箱?如果条件可以,尝试使用ews(exchange web services)吧。
登录owa很麻烦。
如果你真要走owa,用fiddler抓下包,看看到底传了什么数据过去。重要的是,你看看传过去的ID和密码是否被转换了。