使用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的位置不一樣),所以造成服務端反序列化時出現問題.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM