Is there something missing from this (borrowed/ada

2019-09-20 16:42发布

问题:

I posted a question here that sends an HttpWebRequest, but on looking closer at my method SendHTTPRequestNoCredentials(), which I'm sure I adapted from something I found on StackOverflow some time in the past, it seems as if something's missing. See if this looks fishy to you, when the Http method is Get:

public static HttpWebRequest SendHTTPRequestNoCredentials(string uri, HttpMethods method, 
    string data, string contentType)
{
    WebRequest request = null;
    try
    {
        request = WebRequest.Create(uri);
        request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
        request.ContentType = contentType;
        ((HttpWebRequest)request).Accept = contentType;
        ((HttpWebRequest)request).KeepAlive = false;
        ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;

        if (method != HttpMethods.GET && method != HttpMethods.DELETE)
        {
            byte[] arrData = Encoding.UTF8.GetBytes(data);
            request.ContentLength = arrData.Length;
            using (Stream oS = request.GetRequestStream())
            {
                oS.Write(arrData, 0, arrData.Length);
            }
        }
        else
        {
            request.ContentLength = 0;
        }
        //((HttpWebRequest)request). // <= should this call "GetRequestStream())" or 
"BeginGetResponse" or "GetResponse" or "RequestUri" or ... ???
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format(
                "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, 
ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From 
FileXferREST.SendHTTPRequestNoCredentials(): {0}", msgInnerExAndStackTrace));
    }
    return request as HttpWebRequest;
}

Shouldn't there be an explicit "sending" of the HttpWebRequest even when the http type is "Get"? Do I need to add something at the commented out line above, or do I need to change this part:

if (method != HttpMethods.GET && method != HttpMethods.DELETE)
{
    byte[] arrData = Encoding.UTF8.GetBytes(data);
    request.ContentLength = arrData.Length;
    using (Stream oS = request.GetRequestStream())
    {
        oS.Write(arrData, 0, arrData.Length);
    }
}
else
{
    request.ContentLength = 0;
}

...to this:

byte[] arrData = null;
if (method != HttpMethods.GET && method != HttpMethods.DELETE)
{
    arrData = Encoding.UTF8.GetBytes(data);
    request.ContentLength = arrData.Length;
}
else
{
    request.ContentLength = 0;
}
using (Stream oS = request.GetRequestStream())
{
    oS.Write(arrData, 0, arrData.Length);
}

...or else how is the REST method actually called when the http method == GET???

UPDATE

Or should the refactoring really be something like this:

public static HttpWebResponse SendHTTPRequestNoCredentials(string uri, HttpMethods method, 
    string data, string contentType)
{
    . . .
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    return response;
}

(IOW, change the return type from HttpWebRequest to HttpWebResponse, and append those two lines shown above to the end of the method)?

回答1:

The method is set in request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString(); line, so the code snippet looks ok. Call request.GetResponse() or request.BeginGetResponse() to perform the operation.