0

我有 asp.net 应用程序,有 1 个 url 来打开报告,我想尝试使用 apache http 客户端导出此报告。,

 DefaultHttpClient httpclient = new DefaultHttpClient();
     try {

         /* POST login */
         HttpPost httpost = new HttpPost("http://localhost:80");

         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
         nvps.add(new BasicNameValuePair("login", "e"));
         nvps.add(new BasicNameValuePair("pw", "password"));

         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
         HttpResponse response = httpclient.execute(httpost);
         HttpEntity entity = response.getEntity();
         System.out.println("Login form get: " + response.getStatusLine());
         EntityUtils.consume(entity);

         /* get content*/
         HttpGet httpget = new HttpGet("http://localhost:80/Report);

         System.out.println("executing request " + httpget.getURI());

         // Create a response handler
         ResponseHandler<String> responseHandler = new BasicResponseHandler();
         String responseBody = httpclient.execute(httpget, responseHandler);
         System.out.println("----------------------------------------");
         System.out.println(responseBody);
         System.out.println("----------------------------------------");


     } finally {
         // When HttpClient instance is no longer needed,
         // shut down the connection manager to ensure
         // immediate deallocation of all system resources
         httpclient.getConnectionManager().shutdown();
     }
 }

假设 localhost:80/Report 是报告页面,并且会有 1 个按钮将报告导出到 csv。,为此,我需要报告会话和控制 ID,并且经过一些研究,如果我单击导出到 csv,我将获得此获取方法 "/Reserved.ReportViewerWebControl.axd?ReportSession=(need this)Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=(need this)&OpType=Export&FileName=Report+Name&ContentDisposition =OnlyHtmlInline&Format=CSV"

  1. 如何获取报告会话和控件 ID?
  2. 如何导出报告?我已经将 HttpGet 更改为同一会话中的 get 方法并控制 id,但仍然无法正常工作..

我这样做对吗?因为我是一个全新的 apache httpclient ..

4

1 回答 1

0

试试htmlunit.sourceforge.net

@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();

    webClient.closeAllWindows();
}

这是开始链接http://htmlunit.sourceforge.net/gettingStarted.html

于 2013-09-13T10:29:19.130 回答