通过打开的浏览器发送http请求

时间:2016-07-19 11:45:30

标签: java httpurlconnection

我试图从工作中对我们服务器的调用的响应中获取一些值。现在我遇到的问题是,如果我通过浏览器(最好是Firefox)发送相同的请求,我登录到应用程序,它会发回正确的响应,但如果我在浏览器中尝试相同的我没有登录的会话我被重定向到登录页面。如果我尝试直接通过HttpUrlConnection类引发请求,也会发生同样的情况。为了解决问题,我想通过打开的浏览器会话发送请求。这是我目前正在使用的代码:

    String callPage() {
    URL url;
    String Address = "NoValidResponse";
    try {
        url = new URL(getMapWidgetURL()); //function delivers the request URL
        HttpURLConnection Connect = (HttpURLConnection) url.openConnection();   
        //Connect.setInstanceFollowRedirects(false);
        Connect.setRequestMethod("GET"); 
        BufferedReader in = new BufferedReader(new InputStreamReader(Connect.getInputStream()));
        String Source = "";
        String Line;
        while((Line = in.readLine()) != null){
            Source += Line;
            System.out.println(Line);
        }
        System.out.println(Source);
        String[] SourceSplitted = Source.split(","); 
        for (int Zaehler = 0; Zaehler < SourceSplitted.length; Zaehler++) {
            if (SourceSplitted[Zaehler].contains("searchaddress")) { 
                SourceSplitted[Zaehler] = Address;
                break;
            }
        }
    } catch (IOException e) {
        System.out.println("Invalid URL");
        return Address; // Returns"NoValidResponse"
    }
    return Address; 
}

0 个答案:

没有答案