我写的连接代码如下:
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(this.url);
hwr.Method = "GET";
response = (HttpWebResponse)hwr.GetResponse();
但是如果服务器没开的话,response = (HttpWebResponse)hwr.GetResponse();这句就会报错:无法连接到服务器。
我想请问各位高手、大侠
我应该怎么写代码,来判断是否连接到服务器。如果连接到了,在执行response = (HttpWebResponse)hwr.GetResponse();,如果没连接上,就return;
请教各位怎么写判断的代码哦?????
在此先谢谢各位了。
放在一个try catch里不就行了
正如楼上所说,加上try catch就行了
try
{
........
}
catch(Exception ex)
{
MessageBox.Show("服务器连接错误!");
return;
}
引用 正如楼上所说,加上try catch就行了 try { ........ } catch(Exception ex) { MessageBox.Show("服务器连接错误!"); return; } en catch就能捕获这个异常的
先判断Http的状态再获得数据。。。
引用 正如楼上所说,加上try catch就行了 try { ........ } catch(Exception ex) { MessageBox.Show("服务器连接错误!"); return; }
相关问答:
txt 和XML 格式相应 不借助DataSet 导入读取TXT文件 然后直接写入XML(同一文件,不同数据,递增原数据没有被覆盖情况下增加数据.) 教个要点或最好是有个代码提示的 过路好汉 帮个忙撒^^ 不会,帮楼主 ......
急用。联系QQ:8775262,谢谢。 需要进行DllImport的地方直接写DLLImport.方法名。 如:DLLImport.GlobalAddAtom(……); 文件:UShare.pas Delphi(Pascal) code: unit UShare; interface uses Windows, Me ......
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using Syst ......
用"服务器控件名.Style.Add("display","")可以给这控件添加CSS,怎么样才能给读出来呢? 怎么都没人帮忙啊 人都死哪去了 控件ID.Style[HtmlTextWriterStyle.BackgroundColor] 控件 ......