问题出在我的 C# 程序中的 httpwebrequest 方法。 Visual Studio 给它的度量为 60,这相当蹩脚..那么我怎样才能更有效地对其进行编程呢? (:
我的实际代码:
public string httpRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Proxy = WebRequest.DefaultWebProxy;
request.MediaType = "HTTP/1.1";
request.ContentType = "text/xml";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using(StreamReader streamr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
String sresp = streamr.ReadToEnd();
return sresp;
}
感谢您的帮助。 ;)
最佳答案
好吧,首先我不会让数字统治我的代码:)
但是,使用 WebClient
可能会大大简化事情 - 需要计算的代码更少。我不在电脑前,但这看起来像是一个 DownloadString
调用,加上一些请求 header 。
http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=VS.100).aspx
哦,在您创建的所有 IDisposable
对象周围添加一些 using
语句。
关于c# - C# 中的 Codemetric 优化 httpwebrequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527393/