【问题标题】:java.net.MalformedURLException: no protocol: /intl/en/policies/ GET Requestjava.net.MalformedURLException:无协议:/intl/en/policies/ GET 请求
【发布时间】:2013-03-20 10:40:49
【问题描述】:

我一直致力于制作一个简单的程序,该程序遍历页面中的所有链接,访问它们,然后递归。但它似乎一旦出现错误就停止了

java.net.MalformedURLException: no protocol: /intl/en/policies/
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at me.dylan.WebCrawler.WebC.sendGetRequest(WebC.java:67)
at me.dylan.WebCrawler.WebC.<init>(WebC.java:27)
at me.dylan.WebCrawler.WebC.main(WebC.java:36)

我的代码:

package me.dylan.WebCrawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class WebC {
//  FileUtil f;
    int linkamount=0;
    ArrayList<URL> visited = new ArrayList<URL>();
    ArrayList<String> urls = new ArrayList<String>();
    public WebC() {

        try {
//          f= new FileUtil();
            sendGetRequest("http://www.google.com");
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new WebC();
    }
    public void sendGetRequest(String path) throws IOException, BadLocationException, MalformedURLException {

        URL url = new URL(path);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Language", "en-US");
         BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
         EditorKit kit = new HTMLEditorKit();
         HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
         doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
         kit.read(rd, doc, 0);

         //Get all <a> tags (hyperlinks)
         HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
         while (it.isValid())
         {
             MutableAttributeSet mas = (MutableAttributeSet)it.getAttributes();
             //get the HREF attribute value in the <a> tag
             String link = (String)mas.getAttribute(HTML.Attribute.HREF);
             if(link!=null && link!="") {
                 urls.add(link);
             }

             it.next();
         }
         for(int i=urls.size()-1;i>=0;i--) {
             if(urls.get(i)!=null) {
                if(/*f.searchforString(urls.get(i)) ||*/ visited.contains(new URL(urls.get(i)))) {
                    urls.remove(i);
                    continue;
                } else {
                    System.out.println(linkamount++);
                    System.out.println(path);
                    visited.add(new URL(path));
                    //f.write(urls.get(i));
                    sendGetRequest(urls.get(i));
                }
                 try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
             }
         }           
    }
}

老实说,我不知道如何解决这个问题。显然 google 有一个不是有效 url 的 href 标签,我该如何解决这个问题?

【问题讨论】:

    标签: java http get malformedurlexception


    【解决方案1】:

    快速解决方法是在调用之前将urls.get(i) 附加到requestPath。这将给它一个协议和一个域来使用。唯一的问题是,如果您不在循环中扫描当前 url 以查找协议和域,您可能会像这样结束:

    http://www.google.com/http://www.yahoo.com/policies

    【讨论】:

      【解决方案2】:

      您必须在 URL 部分附加 baseURl。 URL 对象要求其格式为 http://abc.com/int/etc/etc

      虽然表单将具有相对格式的格式。简单的方法是在您获得的每个 HREF 中调用 get 之前附加 http://www.google.com

      【讨论】:

      • 弹出另一个问题,代码如下:pastie.org/7164939 我得到:pastie.org/7164948
      • 问题是 www.google.com 末尾的“..”。您想在打开连接之前检查此类边界情况。在这种特殊情况下,您想检查 HREF 是否包含“.”。在附加到链接之前。
      猜你喜欢
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2011-11-22
      • 2018-11-08
      • 1970-01-01
      相关资源
      最近更新 更多