Java-从URL获取页面内容
我做错了什么? 请记住,我通常使用C语言编写代码,而不是java语言。我现在需要将我的c#转换为java Java代码:Java-从URL获取页面内容,java,url,Java,Url,我做错了什么? 请记住,我通常使用C语言编写代码,而不是java语言。我现在需要将我的c#转换为java Java代码: public class HelloWorld{ public static void main(String []args) { System.out.println("Hello World"); boolean output; try { URL url
public class HelloWorld{
public static void main(String []args)
{
System.out.println("Hello World");
boolean output;
try
{
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
output = true;
}
catch(Exception e)
{
output = false;
}
System.out.println("Result: " + Boolean.toString(output));
}
}
C代码为:
using System.Net;
bool output;
try
{
WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString("URL");
output = true;
}
catch
{
output = false;
}
Console.WriteLine("Result: " + output.ToString());
我在Java代码中做错了什么
我收到的错误有:
$javac HelloWorld.java
HelloWorld.java:9: error: cannot find symbol
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
^
symbol: class URL
location: class HelloWorld
HelloWorld.java:9: error: cannot find symbol
URL url = new URL("http://stackoverflow.com/questions/6159118/using-java-to-pull-data-from-a-webpage");
^
symbol: class URL
location: class HelloWorld
HelloWorld.java:10: error: cannot find symbol
URLConnection con = url.openConnection();
^
symbol: class URLConnection
location: class HelloWorld
HelloWorld.java:11: error: cannot find symbol
InputStream is =con.getInputStream();
^
symbol: class InputStream
location: class HelloWorld
4 errors
在这个网站上编译时你的C#代码的第一行是使用System.Net代码>在Java中,您可以导入类。在这里您没有导入或删除。基本上,添加到Java中
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;