此 (borrowed/adapted) 代码中是否缺少某些内容,或者我没有看到 Get 被触发的位置?

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

我发布了一个发送 HttpWebRequest 的问题 ,但仔细观察我的方法 SendHTTPRequestNoCredentials(),我确定我改编自过去一段时间在 Whosebug 上发现的东西,它好像少了什么。当 Http 方法为 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;
}

即使 http 类型是 "Get",HttpWebRequest 也不应该有一个明确的 "sending" 吗?我是否需要在上面注释掉的行中添加一些内容,或者我是否需要更改这部分:

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;
}

...为此:

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

...否则当 http 方法 == GET 时,REST 方法实际上是如何调用的???

更新

或者重构真的应该是这样的:

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

(IOW,将 return 类型从 HttpWebRequest 更改为 HttpWebResponse,并将上面显示的这两行附加到方法的末尾)?

方法设置在request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();行,所以代码片段看起来没问题。调用 request.GetResponse() 或 request.BeginGetResponse() 来执行操作。