无法使用GET方法

时间:2015-08-03 13:34:02

标签: java apache http get

我正在尝试编写一个代码,它将使用java中的GET方法将字符串的内容作为字符串中的响应给出。 下面是我正在使用的代码,它抛出了我java.net.MalformedURLException: no protocol

的错误

如果我尝试在启动网址时插入http://,则会给我Unknown Host Exception.

    String request = ("http://gotoanswer.com/?q=What+is+the+Java+equivalent+for+the+following+in+curl%3F");
    System.out.println(request);
    URL url = null;

    try{
        new URL(request);
        HttpURLConnection connection = (HttpURLConnection) new URL(request).openConnection();           
        connection.setDoOutput(true); 
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("GET"); 
        connection.setRequestProperty("Content-Type", "text/xml"); 
        connection.setRequestProperty("charset", "utf-8");
        connection.connect();

        Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        for ( int c = in.read(); c != -1; c = in.read() )
            System.out.print((char)c);
    }
    catch(IOException e){
        System.out.println(e);
    }

0 个答案:

没有答案