C# -- httpWebRequest 和 httpWebResponse 的使用
结合使用httpWebRequest 和 httpWebResponse,来判断一个网页地址是否可以正常访问。
1.举例
Main( strUrl = <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>WebRequest wreq </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> (<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>WebRequest)WebRequest.Create(strUrl); <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>WebResponse wrsp </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> (<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>WebResponse)wreq.GetResponse(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">if</span> (wrsp.StatusCode ==<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>StatusCode.OK) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(strUrl</span>+ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> --<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a> response正常\r\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(strUrl </span>+ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"> --<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a> response出现异常!\r\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.ReadKey(); }}</span></pre>
2. 运行结果:
总结以上是内存溢出为你收集整理的C# -- HttpWebRequest 和 HttpWebResponse 的使用全部内容,希望文章能够帮你解决C# -- HttpWebRequest 和 HttpWebResponse 的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)