muyan6648
jiangjixiangjava
2017-08-07 03:34
采纳率: 71.4%
浏览 1.7k
已采纳

HttpClient请求问题

这是报错日志
Caused by: java.io.FileNotFoundException: D:\hdm\lh\hbiparent\core\target\core\META-INF\context.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at org.jetbrains.idea.tomcat.TomcatUtil.loadXMLFile(TomcatUtil.java:205)
... 28 moreeManager.jar
2017-08-07 11:07:16,381 [2438700] INFO - ij.compiler.impl.CompileDriver - COMPILATION STARTED (BUILD PROCESS)
2017-08-07 11:07:16,389 [2438708] INFO - j.compiler.server.BuildManager - Using preloaded build process to compile D:\hdm\lh\hbiparent
2017-08-07 11:07:16,764 [2439083] INFO - .server.BuildMessageDispatcher - 远程主机强迫关闭了一个现有的连接。
java.io.IOException: 远程主机强迫关闭了一个现有的连接。

这是代码
public Map getSessionId(String userid )
{
String token = null;
String sessionId = null;
http = new DefaultHttpClient();
try {

        String url = LOGINURL + "&username=" + userid;

        HttpPost post = new HttpPost(url);

        // post.setHeader("HYPLOGIN", userid);
        HttpResponse response = http.execute(post);
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            result.append(line);
        }
        String string = result.toString();
        // System.out.println(string);
        string = string.substring(0, 2500);
        // System.out.println(string);

        String regex = "sToken.*=.*\"(.+?)\";.*sCSRFToken";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(string);
        while (matcher.find()) {
            token = matcher.group(1);
        }
        post.abort();

        // String mtoken = token.replace("\\n", "\\\n") + "=";
        // System.out.println(mtoken);

        HttpPost posthttp = new HttpPost(HTTPURL);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("Application", "FYCWYS"));
        nvps.add(new BasicNameValuePair("isContained", "true"));
        nvps.add(new BasicNameValuePair("isWorkspace", "true"));
        nvps.add(new BasicNameValuePair("LOCALE_LANGUAGE", "zh_CN"));
        nvps.add(new BasicNameValuePair("moduleid", "HyperionPlanning.planning.7"));
        nvps.add(new BasicNameValuePair("themeSelection", "BpmTadpole"));
        nvps.add(new BasicNameValuePair("upk_available", "false"));
        String mtoken = token.replace("\\n", "\\\n") + "=";
        nvps.add(new BasicNameValuePair("sso_token", mtoken));
        posthttp.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse responseSession = http.execute(posthttp);

        posthttp.abort();
        Header[] headers = responseSession.getHeaders("Set-Cookie");
        for (Header header : headers) {
            if (header.toString().contains("SESSIONID")) {
                String str = header.toString();
                // String reg = "Cookie:(.+?);.*path";
                String reg = "JSESSIONID=(.+?);.*path";
                Pattern pattern = Pattern.compile(reg);
                Matcher m = pattern.matcher(str);
                while (m.find()) {
                    sessionId = m.group(1).trim();
                }
            }
        }
        System.out.println(sessionId);

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedOperationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(sessionId);
    Map<String, String> map = new HashMap<>();
     map.put("sessionId",sessionId);


    return map;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • Tombabies
    王搏 2017-08-07 10:10
    已采纳

    可能原因是 你在项目设置里导包 Tomcat启动失败 是因为你没在项目目录里把包放进去

    点赞 评论
  • zy841958835
    cloudyzhao 2017-08-07 06:03

    把代码行数贴出来看看

    点赞 评论
  • qq_35473951
    霸王横千秋 2017-08-07 07:40

    看错误提示信息是,context.xml文件找不到,你看下对应的文件夹里有木有

    点赞 评论
  • muyan6648
    jiangjixiangjava 2017-08-07 08:16

    ** 这是代码**

    public static String getCount() {
    String LOGINURL = "http://ip/workspace/index.jsp?framed=true";
    String HTTPURL = "http://ip/HyperionPlanning/servlet/HspLogOn";
    HttpClient http = null;
    String userid = "admin";
    String token = null;
    String sessionId = null;
    http = new DefaultHttpClient();
    try {

            String url = LOGINURL + "&username=" + userid;
            HttpPost post = new HttpPost(url);
    
            // post.setHeader("HYPLOGIN", userid);
            HttpResponse response = http.execute(post);
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                result.append(line);
            }
            String string = result.toString();
            // System.out.println(string);
            string = string.substring(0, 2500);
            // System.out.println(string);
    
            String regex = "sToken.*=.*\"(.+?)\";.*sCSRFToken";
            Pattern p = Pattern.compile(regex);
            Matcher matcher = p.matcher(string);
            while (matcher.find()) {
                token = matcher.group(1);
            }
            post.abort();
    
            // String mtoken = token.replace("\\n", "\\\n") + "=";
            // System.out.println(mtoken);
    
            HttpPost posthttp = new HttpPost(HTTPURL);
    
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("Application", "FYCWYS"));
            nvps.add(new BasicNameValuePair("isContained", "true"));
            nvps.add(new BasicNameValuePair("isWorkspace", "true"));
            nvps.add(new BasicNameValuePair("LOCALE_LANGUAGE", "zh_CN"));
            nvps.add(new BasicNameValuePair("moduleid", "HyperionPlanning.planning.7"));
            nvps.add(new BasicNameValuePair("themeSelection", "BpmTadpole"));
            nvps.add(new BasicNameValuePair("upk_available", "false"));
            String mtoken = token.replace("\\n", "\\\n") + "=";
            nvps.add(new BasicNameValuePair("sso_token", mtoken));
            posthttp.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
            HttpResponse responseSession = http.execute(posthttp);
    
            posthttp.abort();
            Header[] headers = responseSession.getHeaders("Set-Cookie");
            for (Header header : headers) {
                if (header.toString().contains("SESSIONID")) {
                    String str = header.toString();
                    // String reg = "Cookie:(.+?);.*path";
                    String reg = "JSESSIONID=(.+?);.*path";
                    Pattern pattern = Pattern.compile(reg);
                    Matcher m = pattern.matcher(str);
                    while (m.find()) {
                        sessionId = m.group(1).trim();
                    }
                }
            }
            System.out.println(sessionId);
    
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sessionId);
    return  sessionId;
    }
    
        我在public static void main(String[] args) {
    
        System.out.println( "打印"+getCount());
    }
        能打印出来
    
        用    $.ajax({
            url:"${base.contextPath}/hdm/comsn/query",
            type: 'POST',
            dataType: 'text',
            success: function (data) {
                var obj = eval("("+data+")");
                //这里需要用eval函数,eval() 函数使用的是 JavaScript 编译器,可解析 JSON 文本,然后生成 JavaScript 对象。必须把文本包围在括号中,这样才能避免语法错误:
                alert(obj);
            }
        });
                tomcat就会报错java.lang.ClassNotFoundException: org.apache.http.client.ClientProtocolException
    
    点赞 评论
  • u013254779
    猿小牛 2017-08-07 10:58

    两个问题
    1) 看一下文件路径是否正确
    2) 看一下请求的方法是否需要登陆验证

    点赞 评论

相关推荐