使用URLConnection访问jsp时连接超时

使用URLConnection访问jsp时连接超时

本文介绍了使用URLConnection访问jsp时连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码来自服务器jsp,它访问客户端计算机中的另一个jsp,这与客户端计算机和服务器之间的通讯非常完美,而我的办公室网络中没有任何问题.但是,当我的服务器jsp位于外部办公室(另一个网络)和我的网络中的客户端计算机上时,相同的代码在服务器日志中出现连接超时异常.当我通过ajax调用连接到客户端计算机时,我能够从客户端获得响应.我的代码如下.

My code is from server jsp accessing another jsp which is in clients machine and this is perfectly communicates between client machines and server with in my office network without any issue. But the same code when my server jsp is in outside office(another network) and client machines in my network, am getting connection timeout exception in server logs.And when I connect to client machine through ajax call, i am able to get response form client.my code is below.

服务器jsp中的Java代码:

Java code in server jsp:

    URL jspUrl = new URL("...../Test.jsp");
    URLConnection servletConnection = jspUrl.openConnection();
    servletConnection.setDoOutput(true);
    servletConnection.setDoInput(true);
    servletConnection.setUseCaches(true);
    servletConnection.setDefaultUseCaches(true);
    servletConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");
    OutputStream outputStream = servletConnection.getOutputStream();
    ObjectOutputStream outputToServlet = new ObjectOutputStream(outputStream);
    outputToServlet.writeObject(object);
    outputToServlet.flush();

    InputStream inputStream = servletConnection.getInputStream();
    ObjectInputStream outputFromServlet = new ObjectInputStream(inputStream);
    readObject = outputFromServlet.readObject();
    outputFromServlet.close();
    outputToServlet.close();

正在运行的服务器jsp中的Ajax代码:

Ajax code in server jsp which is working:

function callAjax
{
     if(window.XMLHttpRequest)
     {
          reqObj=new XMLHttpRequest();
     }
     else
     {
          reqObj=new ActiveXObject("Microsoft.XMLHTTP");
     }
     reqObj.onreadystatechange=processfunction;
     reqObj.open("POST","./jspName.jsp?"+Id,false);
     reqObj.send(null);
}

function processfunction()
{
    try
    {
        if(reqObj.readyState==4)
        {
            if(reqObj.status == 200)
            {
                var responseString = reqObj.responseText;

            }
        }
    }
    catch(e)
    {
        //alert(e);
    }

}

推荐答案

首先,请确保您已连接到Internet.然后将连接时间更改为零,即无限:

First of all, make sure you are connected to Internet. Then change connection time to zero i.e infinte:

servletConnection.setConnectTimeout(0);

//Set read time out to zero if required
servletConnection.setReadTimeout(0);

这篇关于使用URLConnection访问jsp时连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:33