首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.net.MalformedURLException:无协议: /intl/en/policies/ GET请求

java.net.MalformedURLException:无协议: /intl/en/policies/ GET请求
EN

Stack Overflow用户
提问于 2013-03-30 01:20:17
回答 2查看 7K关注 0票数 2

我一直致力于编写一个简单的程序,它可以遍历页面中的所有链接,并访问它们,然后递归。但它似乎一运行就会停止,并出现错误

代码语言:javascript
复制
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)

我的代码:

代码语言:javascript
复制
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有一个href标签,它不是一个有效的url,我该如何解决这个问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-30 01:25:01

您必须在URL节中附加baseURl。URL对象期望它的格式为http://abc.com/int/etc/etc

虽然表单的格式为relative format.Easy,但解决方法是在调用每个HREF中的http://www.google.com之前附加get。

票数 4
EN

Stack Overflow用户

发布于 2013-03-30 01:26:42

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15708165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档