java调用http接口问题 2C

程序里调用一个接口,昨天跑的时候还好好的,今天出现了这个问题: java.net.SocketException: Unexpected end of file from server。
这是代码:

//登录
HttpURLConnection loginConn = connect("http://192.168.0.53:20080/glsx/msg_plat/websvr/user/login?uct=xianyukobe@163.com&pwd=123456");
BufferedReader loginReader = new BufferedReader(new InputStreamReader(
loginConn.getInputStream()));
StringBuffer loginBuffer = buffer(loginReader);
System.out.println("登录返回:"+ loginBuffer.toString());
loginReader.close();
JSONObject jo2 = JSONObject.fromObject(loginBuffer.toString());
String ssn = jo2.get("ssn").toString();

    //创建应用
    StringBuffer sb = new StringBuffer("http://192.168.0.53:20080/glsx/msg_plat/websvr/appkey/create_appkey?appnm=app11&appurl=/operating-icp/images/gpush/question-icon.png&ssn=");
    sb.append(ssn);
    HttpURLConnection createConn = connect(sb.toString());
    BufferedReader createReader = new BufferedReader(new InputStreamReader(
    createConn.getInputStream()));
    StringBuffer createBuffer = buffer(createReader);
    System.out.println("创建应用返回:"+ createBuffer.toString());
    createReader.close();
    JSONObject jsonObject = JSONObject.fromObject(createBuffer.toString());
    System.out.println(jsonObject.get("ret"));
 其中分别调用了登录接口和创建应用接口,经过调试发现,登录接口没问题,但是创建应用接口有问题,就是上面说的那个问题。请问有谁遇到过吗?

9个回答

检查一下服务器启动了吗?另外换个客户端访问试试呢
,也可能是网络造成的,创建应用接口的那个问题也是报这个错?

qq_27278103
justdoit_2020 服务器已经启动。我是调用了两个接口,一个是登录接口,一个是创建应用接口。昨天还是可以跑起来的,今天跑就报那个错,调试发现是在创建应用接口出错的,登录没问题。
3 年多之前 回复

你把你的本地ip换成127.0.0.1试试!

qq_27278103
justdoit_2020 还是不行啊
3 年多之前 回复

异常信息全部贴出来,这种情况有可能是网络超时,网络原因造成的。

StringBuffer loginBuffer = buffer(loginReader);
System.out.println("登录返回:"+ loginBuffer.toString());
loginReader.close();
JSONObject jo2 = JSONObject.fromObject(loginBuffer.toString());
String ssn = jo2.get("ssn").toString();
注---------------------------------------------
是不是因为你前面使用loginReader.close();将reader已经关闭,
后面再使用其上层loginBuffer,故报错了,试一下,应该是这个问题

zjn640322
阿理斯特 回复咸鱼Melon: 500是服务器的问题啊
3 年多之前 回复
zjn640322
阿理斯特 StringBuffer sb = new StringBuffer("http://192.168.0.5320080/glsx/msg_plat/websvr/appkey/create_appkey?appnm=app11&appurl=/operating-icp/images/gpush/question-icon.png&ssn="); sb.append(son);先不要使用sb.append()这个方法去拼接url 字符串,new StringBuffer()中的url字符串先用一个正常的url字符串替换测试一下,看看是否能够运行
3 年多之前 回复
zjn640322
阿理斯特 StringBuffer sb = new StringBuffer("http://192.168.0.5320080/glsx/msg_plat/websvr/appkey/create_appkey?appnm=app11&appurl=/operating-icp/images/gpush/question-icon.png&ssn="); sb.append(ssn);
3 年多之前 回复
qq_27278103
justdoit_2020 不行
3 年多之前 回复

//创建应用
StringBuffer sb = new StringBuffer("http://192.168.0.53:20080/glsx/msg_plat/websvr/appkey/create_appkey?appnm=app11&appurl=/operating-icp/images/gpush/question-icon.png&ssn=");
你把这个改成这样试试
//创建应用
StringBuffer sb = new StringBuffer("http://192.168.0.53:20080/glsx/msg_plat/websvr/appkey/create_appkey?appnm=app11&appurl=/operating-icp/images/gpush/q
uestion-icon.png&ssn="**+ssn**);

qq_27278103
justdoit_2020 没看懂
3 年多之前 回复

这个程序昨天跑没问的。今天出现了上面的问题,刚又来按你们的试了试,又出现这个问题2016-12-30 17:59:39.271:WARN::/operating-icp/gpush/gcreate.do: java.net.ConnectException: Connection timed out: connect。。。。我感觉不是程序的问题,而是网络的问题。

看一下服务端后台有没有报错,就是主机192.168.0.53的服务日志

看一下服务端后台log

HTTP ERROR 500

Problem accessing /operating-icp/gpush/gcreate.do. Reason:

    Connection timed out: connect
Caused by:

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:997)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:933)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:851)
    at com.glsx.icp.web.business.gpush.GpushController.connect(GpushController.java:29)
    at com.glsx.icp.web.business.gpush.GpushController.gCreate(GpushController.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.filter.CasFilter.doFilter(CasFilter.java:90)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.util.AssertionThreadLocalFilter.doFilter(AssertionThreadLocalFilter.java:73)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.util.HttpServletRequestWrapperFilter.doFilter(HttpServletRequestWrapperFilter.java:92)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:228)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.authentication.AuthenticationFilter.doFilter(AuthenticationFilter.java:130)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:76)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Powered by Jetty:// 

@phypor @_1_1_7_

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问