网络上的一个文件,怎么判断它是本身不存在,还是别的原因:像网络出现问题

解决方案 »

  1.   

    测试网络是联通就可以了啦,用StateObject对象测一下有返回字符串不,   private   void   readCallBack(IAsyncResult   ar) 
     { 
    i++; 
    StateObject   state   =   (StateObject)ar.AsyncState; 
    Socket   handle   =   state.workSocket; 
    int   read   =   handle.EndReceive(ar); 
    if   (read   >   0) 

    string   data   =   Encoding.ASCII.GetString(state.buffer,   0,   read); 
    state.sb.Append(data); 
    handle.BeginReceive(state.buffer,   0,   StateObject.buffSize,   0,   new   AsyncCallback(readCallBack),   state); 

    else 

    if(state.sb.Length> 0) 

    state.sb.Append("     共接收了"   +   i.ToString()   +   "次"); 
    MessageBox.Show(state.sb.ToString()); 

    handle.Close(); 

      

  2.   

    试图下载一个确定存在的文件
    或者向一个确定存在的URL发出Request
    如果没有得到响应,那么就是网络出现问题
      

  3.   

    下面的代码可以知道能不能访问到文件,但有个问题,就是无法得到具体的出错原因
    参考:
    http://topic.csdn.net/u/20080122/21/8cdb3b9d-f335-403f-a62e-55e4ea5aa29d.htmlusing   System.Net; namespace   libinbin 

    class   Program 

    static   int   Main(string[]   args) 

    if   (args.Length   <   1) 

    Console.WriteLine("请输入地址"); 
    return   -1; 
    } string   url   =   args[0].ToString(); 
    System.Net.HttpWebResponse   res   =   null; 
    HttpStatusCode   hsc; try 

    System.Net.HttpWebRequest   myRequest   =   (HttpWebRequest)System.Net.WebRequest.Create(url); 
    myRequest.Method   =   "HEAD"; 
    res   =   (HttpWebResponse)myRequest.GetResponse(); 
    hsc   =   res.StatusCode; 

    catch   (WebException   wex) 
    { } Console.WriteLine(hsc); return   (Convert.ToInt32(hsc)   ==   200)   ?   0   :   Convert.ToInt32(hsc);