如何填充HttpWebRequest标头?

时间:2016-01-27 08:14:49

标签: c# visual-studio unit-testing

我正在尝试使用以下代码:

local loopstart = tick()
for i = 1, 10 do
    local a = math.sin(tick() - loopstart)+1
    print(a)
    wait()
end

但是运行单元测试会在此部分引发异常:HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; request.Headers.Add("Authorization", "Bearer " + authToken.token.access_token); request.Accept = "application/json"; 因为request.Headers.Add()request.Headers。尽管在我的测试中初始化了null

Headers

ShimHttpWebRequest request = new ShimHttpWebRequest(); ShimWebRequest.CreateString = (urio) => { request.Instance.Headers = new WebHeaderCollection { {"Authorization", "Bearer abcd1234"} }; //also tried initilizing it like this: //WebHeaderCollection headers = new WebHeaderCollection(); //headers[HttpRequestHeader.Authorization] = "Bearer abcd1234"; //request.Instance.Headers = headers; return request.Instance; }; 仍为request.Instance.Headers

我错过了什么?

3 个答案:

答案 0 :(得分:1)

我通过为Headers创建一个getter来解决这个问题,以便它返回WebHeaderCollection而不是null

ShimHttpWebRequest request = new ShimHttpWebRequest();
ShimWebRequest.CreateString = (urio) => request.Instance;
request.HeadersGet = () => {
   WebHeaderCollection headers = new WebHeaderCollection();
   headers.Add("Authorization", "Bearer abcd1234");
   return headers;
};

答案 1 :(得分:0)

我通过如下实例化ShimHttpWebRequest的Header属性来解决此问题,

var httpWebRequest = new ShimHttpWebRequest() { HeadersGet = () => new WebHeaderCollection() };
ShimWebRequest.CreateString = (arg1) => httpWebRequest.Instance;

答案 2 :(得分:-2)

这是我的代码,您可以尝试一下:

public static string HttpPostWebRequest(string requestUrl, int timeout, string requestXML, bool isPost, string encoding, out string msg)
    {

        msg = string.Empty;
        string result = string.Empty;

        try
        {

            byte[] bytes = System.Text.Encoding.GetEncoding(encoding).GetBytes(requestXML);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = requestUrl;
            request.Method = isPost ? "POST" : "GET";
            request.ContentLength = bytes.Length;
            request.Timeout = timeout * 1000;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            if (responseStream != null)
            {
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(encoding));
                result = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                request.Abort();
                response.Close();
                return result.Trim();
            }

        }
        catch (Exception ex)
        {
            msg = ex.Message + ex.StackTrace;
        }

        return result;

    }