zoukankan      html  css  js  c++  java
  • C#的post请求 捕获错误代码的内容

    public string Query(string queryString)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(AOPResource.LVSRequestURL);
                    request.Method = "POST";
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] byteArray = encoding.GetBytes(queryString);
    
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = byteArray.Length;
    
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(byteArray, 0, byteArray.Length);
                    WebResponse response = request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(responseStream);
    
                    string result = sr.ReadToEnd();
                    requestStream.Close();
                    sr.Close();
                    responseStream.Close();
                    return result;
                }
                catch (WebException e)
                {
                    using (WebResponse response = e.Response)
                    {
                        HttpWebResponse httpResponse = (HttpWebResponse)response;
                        Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                        using (Stream data = response.GetResponseStream())
                        using (var reader = new StreamReader(data))
                        {
                            string text = reader.ReadToEnd();
                            throw new Exception(text);
                        }
                    }
                }
            }
  • 相关阅读:
    python 代码片段8
    python 代码片段7
    python 代码片段6
    python 代码片段5
    python 代码片段4
    django 代码片段3
    python 代码片段2
    Redis事物
    Redis的java客户端jedis
    Redis五大数据类型
  • 原文地址:https://www.cnblogs.com/sherlock99/p/4282347.html
Copyright © 2011-2022 走看看