当主URL使用HTTPRequest失败时,使用辅助URL的最佳方法是什么

时间:2018-12-06 03:21:08

标签: c#

public string Post(string url,Request request)
    {
        try
        {
            WebRequest webRequest = WebRequest.CreateHttp(url);
            string postData = JsonConvert.SerializeObject(request);
            byte[] data = Encoding.UTF8.GetBytes(postData);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            webRequest.ContentLength = data.Length;
            using (Stream stream = webRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            string response = string.Empty;
            **WebResponse myHttpWebResponse = webRequest.GetResponse();**
                //what is the best way to use a secondary url if WebResponse is null..
            using (StreamReader sr = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8))
            {
                response = sr.ReadToEnd();

            }
            return response;
        }
        catch (Exception e)
        {
            throw e;
        }
    }

0 个答案:

没有答案