zoukankan      html  css  js  c++  java
  • 使用main方法调用http请求本地服务器的某个servlet报错问题

    java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8081/test/myServlet

    java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8081/test/myServlet

    但是自己却可以用浏览器访问,发现可能是服务器对我们这种java程序屏蔽了。

    因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent

    url = new URL("http://localhost:8081/test/myServlet");
                HttpURLConnection connection = (HttpURLConnection) url.
                    openConnection();
                connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

    这样就可以访问了。


    Exception in thread "main" com.caucho.hessian.client.HessianConnectionException: 500: java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8081/test/myServlet

    这是你引用的jar包引起的,如果你使用上面的例子,而使用hessian-3.1.5.jar或更高版本的包就会出现上述错误。更换hessian-3.0.20.jar以下的包就可以了。 
    至于具体的原因我也没有调查过。

     

     


    运行客户端程序,抛出异常:

    Exception in thread "main" com.caucho.hessian.client.HessianConnectionException: 500: java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8081/test/myServlet

    at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:202)
    at $Proxy0.getPerson(Unknown Source)
    at example.BasicClient.main(BasicClient.java:25)

    查看服务器日志,得到异常信息:
    java.lang.IllegalStateException: Serialized class example.Person must implement java.io.Serializable
    at com.caucho.hessian.io.SerializerFactory.getDefaultSerializer(SerializerFactory.java:262)
    at com.caucho.hessian.io.SerializerFactory.getSerializer(SerializerFactory.java:234)
    at com.caucho.hessian.io.Hessian2Output.writeObject(Hessian2Output.java:406)
    ...

    异常提示 example.Person 必须实现 java.io.Serializable 接口。

    (2)Serializable 和序列化
    对于简单数据类型,如int, double, char,数组等,以及常用的一些类型,如String,java.util.Date,List, Map等,Hessian 本身对其进行了特殊处理,也就是 Hessian 对其进行了序列化操作,但是 Hessian 不可能了解其他的类以及在您的应用程序中使用的那些类。对于这些类,Hessian 则要求这些类本身能够被序列化,也就是要求这些必须实现 Serializable 接口。

    (3)正确的做法
    让 Person 实现 Serializable 接口,则 Person.java 应该被修改成:
    package example;

    import java.io.Serializable;
    import java.util.Date;

    public class Person implements Serializable{
    private int id = 0;
    private String name = "";
    private Date birthday = null;
    ...

    重新启动 Web 服务器,就可以成功运行客户端程序了。

     

    1,org.springframework.remoting.RemoteAccessException: Cannot access Hessian service at [http://61.152.162.173/remote/remoteService]; 
    出现这个异常一般是因为服务端操作出现异常引起的

    2,com.caucho.hessian.io.HessianProtocolException: 501: java.io.IOException: Server returned HTTP response code: 501 for URL: 

    出现这个原因,可能是因为代理问题(我的机器是通过squid代理上网的,并不是通过路由器),501服务器无法提供对请求中所要求功能的支持。如果服务器无法识别请求方法就会回应此状态代码,这意味着不能回应请求所要求的任何资源。

    3,org.springframework.remoting.RemoteConnectFailureException: Cannot connect to Hessian service at http://localhost:8081/test/myServlet]; nested exception is java.net.ConnectException: Connection refused: connect
    连接不上hessian服务器.

    4,客户端抛出的异常:
    Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8081/test/myServlet

    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1174)
    服务端抛出的异常如下:
    [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/].[FileServlet]] - Servlet.service() for servlet FileServlet threw exception
    com.caucho.hessian.io.HessianProtocolException: upload: expected end of call ('z') at ' 
    解决:因为我的服务端要求上传的文件必须在userfiles目录下(代码:filePath.indexOf("userfiles");),判断我之前测试的文件没有放到该目录下,就出现了这种错误.

    5,为什么客户端是对象,到了服务端就是map了呢?????
    原因:我的list在上传前保存的是对象,经测试也不是map型,但到服务端从list获取的变成了map型,经分析是因为目录结构的原因,我的客户端po放到了domain目录下,服务端po放到domainobject下,我是用netCourseInfo组装信息的,客户端和服务端这两个文件不同(因为import的po的位置不一样),所以造成服务端反序列化时出现问题.

  • 相关阅读:
    bootstap 折叠
    AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)
    Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
    BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
    BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
    BZOJ 2759 一个动态树好题 (LCT)
    Codeforces 1205C Palindromic Paths (交互题、DP)
    getopt实现传参自动识别
    powershell笔记
    bat语法需要注意的地方
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/5695441.html
Copyright © 2011-2022 走看看