使用HTTPClient模拟网络对话,始终会出现401未经授权的错误

时间:2019-03-01 07:56:17

标签: apache-httpclient-4.x dynatrace

我有一个Dynatrace NAM产品/网络应用程序。手动登录后,我可以通过鼠标执行以下步骤来导出浏览器中显示的数据。

  1. 登录。http://10.96.1.232/
  2. 点击Reports:网址为http://10.96.1.232/index,它会自动重定向到http://10.96.1.232/LSServlet?dmiAction=Generate&lsAction=LoadByName&lsEntryName=Application%20Health%20Status
  3. 单击DMI:数据挖掘报告:http://10.96.1.232/dashboard?_callUrl=menu_reports_dashboard
  4. 点击Servershttp://10.96.1.232/LSServlet?lsEntryName=Servers&dmiAction=Generate&pUtil_width_=1280&pUtil_height_=615&
  5. 最后单击Export Data,它将在浏览器中显示数据,URL为:http://10.96.1.232/IntegratedDMI?pID_=1551421208481-1154896%40APMAMD02%3A80&pexportSection_=s1&dmiAction=Export&startTime=1551369600000&endTime=1551419700000

要求是我将最后一步中的数据导出到.csv文件中,并上传到splunk进行索引。

我的解决方案是使用httpclient进行最后一步来模拟步骤/会话,解析响应,提取数据并插入csv文件中。

因此,我的代码如下/2/所示,我得到了来自服务器的未授权错误代码401的响应,列在/1/中。

任何提示都可以模拟httpclient和Dynatrace NAM服务器之间的对话,以便我可以解析导出数据页面并提取所需的数据。

/ 1 /

[root@xxx xxx]# java -jar DTExtractData-1.0-jar-with-dependencies.jar 

executing request to http://10.96.1.232:80
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
----------------------------------------
HTTP/1.1 401 Unauthorized
Set-Cookie: JSESStieuvg80=001073DE0541424E75B9597F9D36D254; Path=/; HttpOnly
wrongLoginStatus: 401
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Vary: Accept-Encoding
Date: Fri, 01 Mar 2019 06:57:30 GMT
Server:  
----------------------------------------

<!DOCTYPE html>

<html>
<head>
    <!-- MUST be the first line inside head tag -->
    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <script type='text/javascript'>var $THIS$ = {product:{v:"12.3.7.11",fingerprint:"12.3.7.11.00644d5485eee21d3ed417e51adff961c6dee813",whoami:"Central Analysis Server 12.3.7 (build 19)",app:"rtm"},user:{lang:"en",user:null}}</script>
    ...   

/ 2 / source codes:

package com.xxxxx.xxxxx.extractdata;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class DataMain {

    public static void main(String[] args) {

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("xxxxx", "xxxxx");
        provider.setCredentials(new AuthScope(new HttpHost("10.96.1.232", 80, "http")), credentials);


        HttpClient httpclient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

        try {

            // specify the host, protocol, and port
            HttpHost target = new HttpHost("10.96.1.232", 80, "http");

            // specify the get request
            //String url= "/LSServlet?dmiAction=Generate&lsAction=LoadByName&lsEntryName=Application%20Health%20Status";  
            String url = "/index";          
            HttpGet getRequest = new HttpGet(url);

            System.out.println("executing request to " + target);

            HttpResponse httpResponse = httpclient.execute(target, getRequest);
            HttpEntity entity = httpResponse.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(httpResponse.getStatusLine());
            Header[] headers = httpResponse.getAllHeaders();
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

0 个答案:

没有答案