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

如何通过Java Apache HttpClient使用http导入/导出csv,java,asp.net,apache,httpclient,reportviewer2008,Java,Asp.net,Apache,Httpclient,Reportviewer2008,我有一个asp.net应用程序,它有一个url来打开报表,, 我想尝试使用apache http客户端导出此报表 DefaultHttpClient httpclient = new DefaultHttpClient(); try { /* POST login */ HttpPost httpost = new HttpPost("http://localhost:80"); List <NameValuePair&

我有一个asp.net应用程序,它有一个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();
     }
 }
DefaultHttpClient-httpclient=newdefaulthttpclient();
试一试{
/*登录后*/
HttpPost HttpPost=新的HttpPost(“http://localhost:80");
List-nvps=newarraylist();
添加(新的BasicNameValuePair(“登录”,“e”));
添加(新的BasicNameValuePair(“密码”);
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse response=httpclient.execute(httpost);
HttpEntity=response.getEntity();
System.out.println(“登录表单get:+response.getStatusLine());
EntityUtils.consume(实体);
/*得到满足*/
HttpGet HttpGet=新的HttpGet(“http://localhost:80/Report);
System.out.println(“正在执行请求”+httpget.getURI());
//创建响应处理程序
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httpget,responseHandler);
System.out.println(“--------------------------------------------------------”;
系统输出打印LN(响应库);
System.out.println(“--------------------------------------------------------”;
}最后{
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
httpclient.getConnectionManager().shutdown();
}
}
假设localhost:80/Report是报告页,, 将有一个按钮将报告导出到csv。为此,我需要报告会话和控件id,, 经过一些研究,如果我点击导出到csv,我会得到这个get方法 “/Reserved.ReportViewerWebControl.axd?ReportSession=(需要此项)Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=(需要此项)&OpType=Export&FileName=Report+Name&ContentDisposition=onlyhtmline&Format=CSV”

  • 如何获取报表会话和控件id
  • 如何导出报告?我已经将HttpGet更改为同一会话和控件id中的get方法,但仍然不起作用
  • 我这样做是对的吗?因为我是ApacheHttpClient中的一个全新成员..

    尝试


    下面是getting start link

    一个更好的方法/设计,在asp.net中创建一个web服务,它公开csv导出方法,您可以轻松调用该方法。@UmairSaleem我不能这样做。在这种情况下,我不允许更改或向asp.net应用程序添加任何代码。您是否尝试过HtmlUnit?它是一个“Java程序的无GUI浏览器”“。它为HTML文档建模,并提供一个API,允许您调用页面、填写表单、单击链接等。。。就像你在“普通”浏览器中所做的一样。@UmairSaleem你的建议似乎是一种更简单的方法。我先试试。会让你知道它是否有效。谢谢。:)@不幸的是,我仅限于使用httpClient,,不幸的是,我仅限于使用httpClient
    @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();
    }