Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载网页。wget正常,java失败_Java_Url_Networking_Wget - Fatal编程技术网

下载网页。wget正常,java失败

下载网页。wget正常,java失败,java,url,networking,wget,Java,Url,Networking,Wget,我正在尝试下载以下页面: wget无任何选项失败: wget "http://structureddata.wikispaces.com/Test" (...) connect to session.wikispaces.com insecurely, use `--no-check-certificate' 如果没有支票证书,它就可以工作 wget --no-check-certificate "http://structureddata.wikispaces.com/Test" grep

我正在尝试下载以下页面:

wget无任何选项失败:

wget "http://structureddata.wikispaces.com/Test"
(...) connect to session.wikispaces.com insecurely, use `--no-check-certificate'
如果没有支票证书,它就可以工作

wget --no-check-certificate "http://structureddata.wikispaces.com/Test"
grep Hello Test
 Hello World
现在,我想用java下载相同的URL,但是下面是一个简单的程序:

import java.net.*;
import java.io.*;
public class Test
        {
        public static void main(String args[])
                {
                int c;
                try
                        {
                        InputStream in=new URL("http://structureddata.wikispaces.com/Test").openStream();
                        while((c=in.read())!=-1) System.out.print((char)c);
                        in.close();
                        }
                catch(Throwable err)
                        {
                        err.printStackTrace();
                        }
                }
        }
一无所获

我应该怎样用java下载页面

非常感谢,


Ppierre

Java URL接口相当低级;它不会自动执行跟踪重定向之类的操作。上面的代码没有要打印的内容,因为没有内容

通过像下面这样做,您将看到您得到的是一个HTTP302响应——一个重定向

  URL url = new URL("http://structureddata.wikispaces.com/Test");

  URLConnection urlConnection = url.openConnection();
  Map<String, List<String>> headers = urlConnection.getHeaderFields();
  Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
  for (Map.Entry<String, List<String>> entry : entrySet) {
    String headerName = entry.getKey();
    System.out.println("Header Name:" + headerName);
    List<String> headerValues = entry.getValue();
    for (String value : headerValues) {
      System.out.print("Header value:" + value);
    }
    System.out.println();
    System.out.println();
  }
URL=新URL(“http://structureddata.wikispaces.com/Test");
URLConnection URLConnection=url.openConnection();
Map headers=urlConnection.getHeaderFields();
Set entrySet=headers.entrySet();
for(Map.Entry:entrySet){
字符串headerName=entry.getKey();
System.out.println(“标题名称:“+headerName”);
List HeaderValue=entry.getValue();
for(字符串值:headerValue){
系统输出打印(“标题值:”+值);
}
System.out.println();
System.out.println();
}
我建议使用这样的库,它将为您处理更多协议

(信用卡到期:复制了上面的代码。)

您可能需要查看,此代码返回页面没有问题

final HttpClient client = new HttpClient();
final GetMethod method = new GetMethod("http://structureddata.wikispaces.com/Test");
try {
    if (HttpStatus.SC_OK == client.executeMethod(method)) {
        System.out.println(IOUtils.toString(method.getResponseBodyAsStream()));
    } else {
        throw new IOException("Unable to load page, error " + method.getStatusLine());
    }
} finally {
    method.releaseConnection();
}

问题是它返回一个
302
重定向响应到
https
url。由于初始请求是
http
,而目标是
https
,因此
URLConnection
不会自动跟随重定向(但是,当目标使用相同的方案时会这样做)

经过一些观察,我得出结论,它转到
https
请求一些身份验证令牌,而这些令牌又被重定向到
http
url,身份验证令牌作为请求参数。因此,它应该遵循从
http
https
的重定向,然后是带有实际页面内容的
http

以下内容在这里起作用

public static void main(String... args) throws Exception {
    // First request.
    URLConnection connection = new URL("http://structureddata.wikispaces.com/Test").openConnection();

    // Go to the redirected https page to obtain authentication token.
    connection = new URL(connection.getHeaderField("location")).openConnection();

    // Re-request the http page with the authentication token.
    connection = new URL(connection.getHeaderField("location")).openConnection();

    // Show page.
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        for (String line; ((line = reader.readLine()) != null);) {
            System.out.println(line);
        }
    } finally {
        if (reader != null) try { reader.close(); } catch (IOException ignore) {}
    }
}

不过,我确实同意这是一个更好的工作工具。

有点奇怪:错误表明您正在使用https,这与给定的URL不一致。我无法从wget复制消息。是否涉及代理服务器?它会自动跟踪重定向,只有当它涉及不同的方案时才不会。通过
((HttpURLConnection)urlConnection.getFollowRedirects()
检查您自己。