有什么是从这里(借/改编)code缺失,还是我没有看到其中获取被解雇? [英] Is there something missing from this (borrowed/adapted) code, or am I not seeing where a Get gets fired?

查看:266
本文介绍了有什么是从这里(借/改编)code缺失,还是我没有看到其中获取被解雇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴的问题<一href=\"http://stackoverflow.com/questions/27803761/how-can-i-unpack-a-returned-generic-list-of-a-custom-type-on-the-compact-framewo\">here发送一个HttpWebRequest的,但看着我的方法SendHTT prequestNoCredentials(),我敢肯定,我改编自东西,我在计算器上发现了一些时间在过去的近了,它好像有什么地方丢失了。看看这是腥你,当HTTP方法是获取:

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

不应该有一个明确的HttpWebRequest的的送,甚至当HTTP类型是获取?我需要在注释掉线之上添加的东西,或者我需要改变这一部分:

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

...这样:

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

...要不怎么是REST方法实际上调用时HTTP方法== GET ???

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

还是应该重构真的是这样的:

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,从HttpWebRequest的返回类型更改为HttpWebResponse,和以上所示的那些两行追​​加到方法的结束时)?

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

推荐答案

该方法是设置 request.Method = Enum.ToObject(typeof运算(HttpMethods),法)的ToString(); 行,因此code片段看起来确定。调用request.GetResponse()或request.BeginGetResponse()来执行该操作。

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.

这篇关于有什么是从这里(借/改编)code缺失,还是我没有看到其中获取被解雇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆