[英]Java variable in URL not working
So basically i have a script that's downloading a txt file contents off a website. 所以基本上我有一个脚本,可以从网站上下载txt文件的内容。 It works but i wanted to add variables to the link so it's custom to each user.
它有效,但是我想向链接中添加变量,以便对每个用户进行自定义。 So just as an example i wanted it to be like:
因此,作为一个例子,我希望它像:
String name = "Matt";
String URLComplete = "http://www.domain.com/" + name + "/test.txt";
URL url = new URL(URLComplete);
That doesn't work but if i did 那行不通,但如果我做到了
URL url = new URL("http://www.domain.com/Matt/test.txt");
It would work, i don't understand what i'm doing wrong? 可以,我不明白我在做什么错?
So even if i do the following: 因此,即使我执行以下操作:
String URLComplete = "http://www.domain.com/name/test.txt";
URL url = new URL(URLComplete); URL url =新的URL(URLComplete);
It doesn't work but if i do 这不起作用,但是如果我这样做
URL url = new URL("http://www.domain.com/name/test.txt");
It will work, by work i mean it successfully reads/downloads in the string/array from the website. 它可以工作,通过工作,我的意思是它可以从网站上成功读取/下载字符串/数组。 I've outputted both and the urls are identical no mistakes i've visited both through my browser and i end up at the correct page.
我已经输出了两个,并且URL是相同的,没有错误,我已经通过浏览器访问了两个URL,最终到达了正确的页面。
Full Code: 完整代码:
This is my full code of retrieving the data. 这是我检索数据的完整代码。
public class Retrieve {
public static List<String> getWhitelist()
{
try
{
String name = "Matt";
String URLComplete = "http://www.domain.com/" + name + "/test.txt";
List<String> strings = new ArrayList();
URL url = new URL(URLComplete);
BufferedReader urlReader = new BufferedReader(new InputStreamReader(url.openStream()));
String s;
while ((s = urlReader.readLine()) != null)
{
strings.add(s);
}
return strings;
}
catch (Exception e) {}
return new ArrayList<String>();
}
public static String downloadString(String link)
{
try
{
URL url = new URL(link);
BufferedReader result = new BufferedReader(new InputStreamReader(url.openStream()));
return result.readLine();
}
catch (Exception e) {}
return "Unable to connect to the internet!";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.