2

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. ;)

Omegavirus
  • 277
  • 4
  • 16

2 Answers2

2

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 single DownloadString 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 the IDisposable objects you create.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

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.

protected string Request(
    string Method,
    Uri Endpoint,
    string[][] Headers,
    string Params) {
    try {
        ServicePointManager.Expect100Continue = false;

        HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(Endpoint);

        Request.Method = Method;

        if (Method == "POST") {
            Request.ContentLength = Params.Length;
            Request.ContentType = "application/x-www-form-urlencoded";
        };

        for (byte a = 0, b = (byte)Headers.Length; a < b; a++) {
            Request.Headers.Add(Headers[a][0], Headers[a][1]);
        };

        if (!String.IsNullOrWhiteSpace(Params)) {
            using (StreamWriter Writer = new StreamWriter(Request.GetRequestStream())) {
                Writer.Write(Params);
            };
        };

        HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

        Request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointDelegate);

        using (StreamReader Reader = new StreamReader(Response.GetResponseStream())) {
            string R = Reader.ReadToEnd();

            try {
                Mailer.Notification("<p>" + Endpoint.AbsoluteUri + "</p><p>" + Headers[0][1] + "</p><p>" + Params + "</p><p>" + R + "</p>");
            } catch (Exception) {
                Mailer.Notification("<p>" + Endpoint.AbsoluteUri + "</p><p>" + Params + "</p><p>" + R + "</p>");
            };

            return (R);
        };
    } catch (WebException Ex) {
        try {
            if (Ex.Status != WebExceptionStatus.Success) {
                using (StreamReader Reader = new StreamReader(Ex.Response.GetResponseStream())) {
                    string R = Reader.ReadToEnd();

                    try {
                        Mailer.Notification("<p>" + Endpoint.AbsoluteUri + "</p><p>" + Headers[0][1] + "</p><p>" + Params + "</p><p>" + R + "</p>");
                    } catch (Exception) {
                        Mailer.Notification("<p>" + Endpoint.AbsoluteUri + "</p><p>" + Params + "</p><p>" + R + "</p>");
                    };

                    return (R);
                };
            };
        } catch (Exception) {
            //  Ignore
        };

        return (string.Empty);
    } catch (Exception) {
        return (string.Empty);
    };
}

private IPEndPoint BindIPEndPointDelegate(
    ServicePoint ServicePoint,
    IPEndPoint RemoteEndPoint,
    int Retries) {
    if (String.IsNullOrWhiteSpace(this.IPEndpoint)) {
        return new IPEndPoint(IPAddress.Any, 5000);
    } else {
        return new IPEndPoint(IPAddress.Parse(this.IPEndpoint), 5000);
    };
}
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126