C# 中的 Codemetric 优化 httpwebrequest
问题是我的 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;
}
感谢您的帮助。;)
the problem is the httpwebrequest method in my c# program. visual studio gives it a metric of 60, thats pretty lame.. so how can i program it more efficient? (:
my actual code:
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;
}
thanks for helping. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先我不会让数字统治我的代码:)
但是,使用
WebClient
可能会大大简化事情 - 需要计算的代码更少。我不在电脑前,但这看起来像是一个DownloadString
调用,加上一些请求标头。http://msdn.microsoft.com/en- us/library/fhd1f0sw(v=VS.100).aspx
哦,在您创建的所有
IDisposable
对象周围添加一些using
语句。Well, firstly I wouldnt let a number rule my code :)
However, using
WebClient
may simplify things quite a bit - less code to be counted. I'm not at a PC but that looks like a singleDownloadString
call, plus a few request headers.http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=VS.100).aspx
Oh, and add some
using
statements around all theIDisposable
objects you create.以下是我在构建的社交网络类中使用的代码,该类与 Twitter、Facebook、Tumblr 等进行交互。根据您的需要进行修改。另外,我不知道 VS 会给出什么“指标”,但如果您指的是“计算代码指标”,60 仍然不错。 20 到 100 被认为是易于维护的,所以我不会担心太多了。
Here's the code I use in a social networking class I built which interacts with Twitter, Facebook, Tumblr, etc. Modify as you see fit. Also, I don't know what "metric" it would be given by VS, but if you're referring to the "Calculate Code Metrics" a 60 is still good. 20 to 100 is considered to be well maintainable, so I wouldn't worry too much.