使用Java代码在Glassfish服务器上请求网页

时间:2011-12-29 09:37:22

标签: java glassfish request

我正在尝试从java代码中读取网页,并且对于像google.com这样的一般网站很好但是当使用相同的代码在我的计算机上阅读glassfish服务器上的页面时,我有这样的错误: -

java.io.IOException: Server returned HTTP response code: 406 for URL: http://jedsms02:8080/Mirnint_Library-Mirnint_Message-context-root/Sending.jsp
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.URL.openStream(URL.java:1010)
at reader.ReadSMS.main(ReadSMS.java:195)

这是我的代码: -

String urlString =
       "http://localhost:8080/Mirnint_Library-Mirnint_Message-context-root/Sending.jsp"
    try {
        URL url;
        url = new URL(urlString);            
        Scanner in = new Scanner(url.openStream());
        while (in.hasNext())
            System.out.println(in.nextLine());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我更改了网址,将localhost替换为IP" 192.120.20.167"但是我得到了这个错误

java.io.IOException: Server returned HTTP response code: 505 for URL: http://192.120.20.167:8080/Mirnint_Library-Mirnint_Message-context-root/Sending.jsp?Number=0xxxxxxxxx&Message= Test Man
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.URL.openStream(URL.java:1010)
at reader.ReadSMS.main(ReadSMS.java:195)

请帮忙, 谢谢

1 个答案:

答案 0 :(得分:0)

您获得了505,因为您的请求参数未编码。使用URLEncoder对参数进行编码,例如

URLEncoder.encode(" Test Man", "utf8");

然后网址变为

http://192.120.20.167:8080/Mirnint_Library-Mirnint_Message-context-root/Sending.jsp?Number=0xxxxxxxxx&Message=%20Test%20Man