java - 如何从 Outlook 电子邮件内容导航到正确的“URL”
问题描述
我正在成功阅读来自 JAVAX 邮件的 Outlook 电子邮件。但是,当我尝试在电子邮件正文中获取“链接”时,它并没有给出确切的 URL,而是给出了带有一些额外字符的 URL,例如“=3D?*/”。我尝试使用下面的代码,但它没有帮助我。
public List<String> getUrlsFromMessage(Message message, String linkText) throws Exception {
String html = getMessageContent(message);
List<String> allMatches = new ArrayList<String>();
// (<a [^>]+>)
Matcher matcher = Pattern.compile(" (<a [^>]+>)" + linkText + "</a>").matcher(html);
while (matcher.find()) {
String aTag = matcher.group(1);
allMatches.add(aTag.substring(aTag.indexOf("http"), aTag.indexOf("\">")));
}
return allMatches;
}
我也将模式更改为
Pattern linkPattern = Pattern.compile(" <a\\b[^>]*href=\"([^\"]*)[^>]*>(.*?)</a>",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);`
但它仍然给了我错误的 URL。
解决方案
最后,我找到了使用 StringBuilder 检索确切 URL 的解决方案。我所做的是我从字符串中删除了不需要的字符,直到我得到正确的 URL。这可能不是一个好的编码实践,但这是唯一适合我的工作。
StringBuilder build = new StringBuilder(link);
build.deleteCharAt(43);// Shift the positions front.
build.deleteCharAt(51);
build.deleteCharAt(51);
driver.get(build.toString());
推荐阅读
- javascript - 用于分页和搜索实时数据的数据表?
- angular - this.loggedIn 几秒后才设置为true,如何等待它的值
- php - 如何选择多个表将mysql转换为json?
- ios - UINavigationBar 背景颜色不显示为纯色
- c++ - 从 C++ 中的文本文件读取后,如何对这些值求和?
- html - Django - Textarea 中断
- javascript - 防止 :hover 冒泡
- wpf - 调整大小时 BitmapCache 性能不佳
- java - 如何在java中将投影EPSG:3857转换为EPSG:4326(地理空间)
- uitableview - 使用 RxSwift 在 UITableView 中进行 2 路绑定