Is there something missing from this (borrowed/adapted) code, or am I not seeing where a Get gets fired?

雨燕双飞 提交于 2019-12-13 22:00:23

问题


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.



来源:https://stackoverflow.com/questions/27804430/is-there-something-missing-from-this-borrowed-adapted-code-or-am-i-not-seeing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!