如何通过Java Apache HttpClient使用http导入/导出csv

时间:2013-09-13 09:32:11

标签: java asp.net apache httpclient reportviewer2008

我有asp.net应用程序,有1个网址打开报告, 我想尝试使用apache http client。,

导出此报告
 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是报告页面, 并且会有一个按钮将报告导出到csv。,为此,我需要报告会话和控制ID, 经过一些研究,如果我点击导出到csv我会得到这个get方法 “/Reserved.ReportViewerWebControl.axd?ReportSession=(need this)Culture = 1033&amp; CultureOverrides = True&amp; UICulture = 1033&amp; UICultureOverrides = True&amp; ReportStack = 1&amp; ControlID =(需要此)&amp; OpType = Export&amp; FileName = Report +名称和安培; ContentDisposition = OnlyHtmlInline&安培;格式= CSV“

  1. 如何获取报告会话和控制ID?
  2. 如何导出报告?我已经将HttpGet更改为同一会话中的get方法并控制id,但仍无法正常工作..
  3. 我这样做对吗?因为我是一个全新的apache httpclient ..

1 个答案:

答案 0 :(得分: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