【问题标题】:HttpURLConnection - get the normal instead of the mobile website in servletHttpURLConnection - 在servlet中获取普通网站而不是移动网站
【发布时间】:2014-10-01 05:14:47
【问题描述】:

我的 Servlet 中有以下 HttpURLConnection:

url = new URL("http://www.example.com/parameter1/parameter2/...");
        HttpURLConnection c = (HttpURLConnection)url.openConnection();  
        c.setRequestMethod("GET");
        c.setReadTimeout(2000);

        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream(),"UTF8"));
        String inputLine;
        StringBuffer res = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        res.append(inputLine);  
        }

我的问题是,网站服务器给我的是移动网站,而不是像我在普通浏览器中获得的普通网站。 我必须通过这个 GET 请求发送什么(以及如何),从服务器获取正常的网站?

【问题讨论】:

    标签: java http url servlets


    【解决方案1】:

    尝试调用

    c.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
    

    它将用户代理属性设置为 Chrome(桌面)浏览器。

    【讨论】:

      【解决方案2】:

      是的!通过键入设置另一个用户代理

      c.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
      

      或其他。

      【讨论】:

        猜你喜欢
        • 2012-01-31
        • 2011-10-26
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        相关资源
        最近更新 更多