代码如下:
                    ServicePointManager.DefaultConnectionLimit = 10;
                    string DataPost = HTMLInfo[0].ToString() + SetpNum;//我要取一个网站的数据,数据是分页的,这里得到参数;
                    DataPost = DataPost.Replace("&", "&");
                    ASCIIEncoding myEncoding = new ASCIIEncoding();
                    byte[] Byte = myEncoding.GetBytes(DataPost);
                    MyRequest.ContentType = "application/x-www-form-urlencoded";
                    MyRequest.Method = "post";
                    MyRequest.ContentLength = DataPost.Length;
                    Stream NewStream = MyRequest.GetRequestStream();
                    NewStream.Write(Byte, 0, Byte.Length);                    HttpWebResponse Response = (HttpWebResponse)MyRequest.GetResponse();//这里有时候会报错;错误类型如下.
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.请问哪位碰到过这种问题,错误是偶尔出现的,我试了几次,可能如果网速慢的话,容易出现这个错误,50分求解,不够再另给啊...急

解决方案 »

  1.   

    static int retry = 0;
    try
    {
    connect();
    }
    catch
    {
    if(retry<n)
    {
    connect();
    interlock.increment(retry);
    }
    }
    网络连接总会出现各种预期不到的连接错误,许多重连一次就行了,当然要设置重连数,以免死循环
      

  2.   

    谢谢啊,不过interlock.increment(retry); 这句是什么意思哪,
      

  3.   

    我没碰到过,我的NewStream.write后有NewStream.close(),加上试试看
      

  4.   

    为什么我在msdn里面找不到interlock,只有interlocked类.
      

  5.   

    我试了一下,还是会出现错误,这次是在catch里面的HttpWebResponse Response = (HttpWebResponse)MyRequest.GetResponse();//出错,if语句和interlock.increment(retry)似乎没有起到左右啊.如果try捕捉到了错误,然后connect(),这次如果再连接失败,程序就会中指了
      

  6.   

    catch
    {
    if(retry<n)
    {
    connect();
    interlock.increment(retry);
    }
    else
    {
    ?
    }
    }
    超过重试次数没有处理,当然出错
      

  7.   

    你要想别人的站点Post数据,数据的格式不能错,而且要有cookietainer,否则人家怎么知道你是正常的访客