调用PDF,Server返回HTTP响应代码:400

时间:2015-07-19 11:58:42

标签: http pdf selenium-webdriver

英语不是我的母语;请原谅输入错误。
希望我改进了这个问题。

  1. 我启动了一个关于Selenium的脚本,自动化测试,WebDriver。
  2. 我在HTML-Page上执行链接
  3. 此链接会创建新的浏览器标签页
  4. 此BrowserTab显示PDF / A标准文档
  5. 我想解析PDF并验证其内容

  6. 在HttpURLConnection之后,我得到了超文本传输​​协议(HTTP)响应状态码:400
    并且无法解析InputStream 在localhost上可以完成。
    我也可以点击链接" curent URL"没有任何问题。
    Selenium v​​2.45.0
    在IE 9和Firefox 39.0上测试。

    我该如何检查问题?
    我读到了HTTP响应代码:400
    但是没有找到解决我问题的方法 这可能是重定向问题吗? 谢谢你 这是我的代码:

    public class httperrorStack {
    public static void main ( String[] args )
    {
        // Selnium WebDriver
        WebDriver driver = null;
        //driver = new FirefoxDriver();
        // Test with Browser IE 9 same Problem withFirefox 39.0
        System.setProperty("webdriver.ie.driver","C:\\dir\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
    
        String baseUrl = "http://##.###.###.##:#####/wps/portal"; //ATU2                
        // launch direct to Base URL
        driver.get(baseUrl);    
        // launch to Documentlink: opens PDF/A-Document in a new Browser Tab
        driver.findElement(By.xpath("//a[@title='document_link']")).click();    
    
        //Get all the window handles in a set
        Set <String> handles =driver.getWindowHandles();
        Iterator<String> it = handles.iterator();
        //iterate through windows
        while (it.hasNext()){
        String parent = it.next();
        String newwin = it.next();
        driver.switchTo().window(newwin);       
    
        URL url = null;
        try {
            // The CurrentURL of the documentlink
            // url = "http://##.###.###.###:#####/wps/myportal/dirname/dirname/dir-name/!dir/p/b1/about50characters/In"
            url = new URL(driver.getCurrentUrl());
        } catch (MalformedURLException e1) {            
            e1.printStackTrace();
        }                
    
        // verify connection
        HttpURLConnection connection = null;
        HttpURLConnection httpConn = (HttpURLConnection)connection;
        try {
            httpConn = (HttpURLConnection)url.openConnection();
        } catch (IOException e1) {            
            e1.printStackTrace();
        }
        InputStream is = null;
    
        // Response Code = 400
        try {
            if (httpConn.getResponseCode() >= 400) {
                is = httpConn.getErrorStream();
                // httpConn.getResponseCode() = 400
                //httpConn.getErrorStream() = "sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1d082e88"
            } else {
                is = httpConn.getInputStream();
            }
        } catch (IOException e1) {
                e1.printStackTrace();
        }              
    
        try{            
            connection = (HttpURLConnection)url.openConnection();                    
            InputStream is2;
            if (connection.getResponseCode() >= 400) {
                is2 = connection.getErrorStream();
                //connection.getErrorStream() = "sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@60704c"                
            } else {
                is2 = connection.getInputStream();
            }                   
    
    
        }
        catch (Exception e){
            e.printStackTrace();
        }
    
        BufferedInputStream fileToParse = null;
        try {
            fileToParse = new BufferedInputStream(
                    url.openStream());
        } catch (IOException e) {
            e.printStackTrace();
            // output of StackTrace
            /*java.io.IOException: Server returned HTTP response code: 400 for URL: http://##.###.###.##:#####/wps/redirect
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
            at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
            at java.net.URL.openStream(Unknown Source)
            at mypackage.httperrorStack.main(httperrorStack.java:134)*/
        }
    
        // Close Window
        driver.close();
        driver.switchTo().window(parent);
        driver.quit();  
        // exit the program explicitly
        System.exit(0);
          }
       }
    }
    

0 个答案:

没有答案