java.net.MalformedURLException: 没有基于 URLEncoder 修改的字符串的 URL 协议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22093864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.MalformedURLException: no protocol on URL based on a string modified with URLEncoder
提问by MorkPork
So I was attempting to use this String in a URL :-
所以我试图在 URL 中使用这个字符串:-
http://site-test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachmentse89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
In this code: -
在这段代码中: -
String fileToDownloadLocation = //The above string
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
But at this point I get the error: -
但此时我收到错误:-
java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah
I realised with a bit of googling this was due to the characters in the URL (guessing the &), so I then added in some code so it now looks like so: -
我通过一些谷歌搜索意识到这是由于 URL 中的字符(猜测 &),所以我然后添加了一些代码,现在看起来像这样: -
String fileToDownloadLocation = //The above string
fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8");
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
However, when I try and run this I get an error when I try and create the URL, the error then reads: -
但是,当我尝试运行它时,当我尝试创建 URL 时出现错误,然后错误显示为:-
java.net.MalformedURLException: no protocol: http%3A%2F%2Fsite-test.testsite.com%2FMeetings%2FIC%2FDownloadDocument%3FmeetingId%3Dc21c905c-8359-4bd6-b864-844709e05754%26itemId%3Da4b724d1-282e-4b36-9d16-d619a807ba67%26file%3D%5C%5Cs604132shvw140%5CTest-Documents%5Cc21c905c-8359-4bd6-b864-844709e05754_attachments%5C7e89c3cb-ce53-4a04-a9ee-1a584e157987%myDoc.pdf
It looks like I can't do the encoding until after I've created the URL else it replaces slashes and things which it shouldn't, but I can't see how I can create the URL with the string and then format it so its suitable for use. I'm not particularly familiar with all this and was hoping someone might be able to point out to me what I'm missing to get string A into a suitably formatted URL to then use with the correct characters replaced?
看起来我在创建 URL 之前无法进行编码,否则它会替换斜杠和它不应该的东西,但是我看不到如何使用字符串创建 URL,然后将其格式化它适合使用。我对这一切并不是特别熟悉,希望有人能够向我指出我缺少什么,以便将字符串 A 转换为格式正确的 URL,然后使用正确的字符替换?
Any suggestions greatly appreciated!
任何建议非常感谢!
采纳答案by m-szalik
You need to encode your parameter's values before concatenating them to URL.
Backslash \
is special character which have to be escaped as %5C
在将参数值连接到 URL 之前,您需要对其进行编码。
反斜杠\
是特殊字符,必须转义为%5C
Escaping example:
逃逸示例:
String paramValue = "param\with\backslash";
String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8");
java.net.URL url = new java.net.URL(yourURLStr);
The result is http://host.com?param=param%5Cwith%5Cbackslash
which is properly formatted url string.
结果是http://host.com?param=param%5Cwith%5Cbackslash
格式正确的 url 字符串。
回答by fge
You want to use URI templates. Look carefully at the README of this project: URLEncoder.encode()
does NOT work for URIs.
您想使用URI 模板。仔细查看该项目的 README:URLEncoder.encode()
不适用于 URI。
Let us take your original URL:
让我们获取您的原始 URL:
http://site-test.test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
http://site-test.test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\s604132shvw140\测试的文档\ c21c905c-8359 -4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
and convert it to a URI template with two variables (on multiple lines for clarity):
并将其转换为具有两个变量的 URI 模板(为清晰起见,在多行中):
http://site-test.test.com/Meetings/IC/DownloadDocument
?meetingId={meetingID}&itemId={itemID}&file={file}
Now let us build a variable map with these three variables using the library mentioned in the link:
现在让我们使用链接中提到的库构建包含这三个变量的变量映射:
final VariableMap = VariableMap.newBuilder()
.addScalarValue("meetingID", "c21c905c-8359-4bd6-b864-844709e05754")
.addScalarValue("itemID", "a4b724d1-282e-4b36-9d16-d619a807ba67e")
.addScalarValue("file", "\\s604132shvw140\Test-Documents"
+ "\c21c905c-8359-4bd6-b864-844709e05754_attachments"
+ "\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf")
.build();
final URITemplate template
= new URITemplate("http://site-test.test.com/Meetings/IC/DownloadDocument"
+ "meetingId={meetingID}&itemId={itemID}&file={file}");
// Generate URL as a String
final String theURL = template.expand(vars);
This is GUARANTEED to return a fully functional URL!
这是保证返回一个功能齐全的 URL!
回答by erhun
I have the same problem, i read the url with an properties file:
我有同样的问题,我用属性文件读取了 url:
String configFile = System.getenv("system.Environment");
if (configFile == null || "".equalsIgnoreCase(configFile.trim())) {
configFile = "dev.properties";
}
// Load properties
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/" + configFile));
//read url from file
apiUrl = properties.getProperty("url").trim();
URL url = new URL(apiUrl);
//throw exception here
URLConnection conn = url.openConnection();
dev.properties
开发属性
url = "https://myDevServer.com/dev/api/gate"
it should be
它应该是
dev.properties
开发属性
url = https://myDevServer.com/dev/api/gate
without "" and my problem is solved.
没有“”,我的问题就解决了。
According to oracle documentation
根据oracle文档
- Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed.
- 抛出以指示出现格式错误的 URL。在规范字符串中找不到合法协议或无法解析字符串。
So it means it is not parsed inside the string.
所以这意味着它没有在字符串内解析。
回答by Dave
Thanks to Erhun's answer I finally realised that my JSON mapper was returning the quotation marks around my data too! I needed to use "asText()" instead of "toString()"
感谢 Erhun 的回答,我终于意识到我的 JSON 映射器也在返回我的数据周围的引号!我需要使用“ asText()”而不是“ toString()”
It's not an uncommon issue - one's brain doesn't see anything wrong with the correct data, surrounded by quotes!
这不是一个罕见的问题 - 一个人的大脑没有发现正确数据有任何问题,被引号包围!
discoveryJson.path("some_endpoint").toString();
"https://what.the.com/heck"
discoveryJson.path("some_endpoint").asText();
https://what.the.com/heck