csharp 模拟HttpWebRequest - SUT

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 模拟HttpWebRequest - SUT相关的知识,希望对你有一定的参考价值。

using System;
using System.IO;
using System.Net;

namespace LeeSoft.TestUtils
{
    public class ExampleClass
    {
        public string RequestData(Uri uri)
        {
            var response = CreateWebRequest(uri).GetResponse();
            var responseStream = response.GetResponseStream();

            if (responseStream == null) return null;

            string responseData;
            using (var reader = new StreamReader(responseStream))
            {
                responseData = reader.ReadToEnd();
            }

            return responseData;
        }

        private static HttpWebRequest CreateWebRequest(Uri uri)
        {
            var request = WebRequest.Create(uri) as HttpWebRequest;

            if (request == null) return null;

            request.ContentType = "text/xml;charset=\"utf-8\"";
            request.Method = "GET";
            request.Timeout = 1000;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;

            return request;
        }
    }
}

以上是关于csharp 模拟HttpWebRequest - SUT的主要内容,如果未能解决你的问题,请参考以下文章

csharp 模拟HttpWebRequest - SUT

csharp HttpWebRequest的提交后数据

markdown 模拟HttpWebRequest

HttpWebRequest 保存Cookies,模拟Session登录

利用HttpWebRequest模拟表单提交

C#,HttpWebRequest模拟发送Post请求