各位高手大家好!
    请问下在用java HttpURLConection类post数据时刚开时能够顺利完成但是如果联系多次进行就被卡在getInputStream()处无法运行下去,不知道大家在运用时有没有碰到,希望大侠指点。
  StringBuffer result = new StringBuffer();
        URL httpurl=null;
        HttpURLConnection httpConn=null;
        try {
            httpurl = new URL(url);
            httpConn = (HttpURLConnection) httpurl.openConnection();
            httpConn.setRequestMethod("POST");
            //httpConn.setReadTimeout(5000);
            httpConn.setConnectTimeout(30000);
             httpConn.setUseCaches(false);            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect(); 
            OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
            outw.write("line=" + line + "");
            outw.flush();
            outw.close();
            //InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
           /*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
            BufferedReader ins = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
           // BufferedReader ins = new BufferedReader(re);            String lines;
            while ((lines = ins.readLine()) != null) {
                result.append(lines);
            }
            ins.close();
            httpConn.connect();

解决方案 »

  1.   


    public static void main(String[] args) {
    // TODO Auto-generated method stub
    StringBuffer result = new StringBuffer();
    URL httpurl=null;
    HttpURLConnection httpConn=null;
    try {
    httpurl = new URL("http://www.123.com");
    httpConn = (HttpURLConnection) httpurl.openConnection();
    httpConn.setRequestMethod("POST");
    // httpConn.setReadTimeout(5000);
    httpConn.setConnectTimeout(30000);
    httpConn.setUseCaches(false);

    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.connect(); 
    // OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
    // outw.write("keywords=help");
    // outw.flush();
    // outw.close();
    // InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
    /*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
    BufferedReader ins = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
    //  BufferedReader ins = new BufferedReader(re); String lines;
    while ((lines = ins.readLine()) != null) {
    result.append(lines);
    }
    System.out.println(result.toString());
    ins.close();
    httpConn.connect();
    } catch (IOException e) {
    System.out.println(e);
    } }能行的,我运行没有卡住.你再试试
      

  2.   

    没遇到过类似错误,我只做过获取方面的!没遇过post的,学习!
      

  3.   


    刚开始是可以运行,连续多次调用后就不能运行下去了,如果设定read时间httpConn.setReadTimeout(5000);时间到后就抛出异常 Read timed out,url为本地的一个jsp页面的url,不知道什么原因