十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误

一、远程服务器返回错误: (400) 错误的请求错误

捕获异常查看具体错误

 try
            {

                var mRequest = (HttpWebRequest)WebRequest.Create(url);
                //相应请求的参数
                var data = Encoding.GetEncoding(encoding).GetBytes(parms);
                mRequest.Method = "Post";
                mRequest.ContentType = contentType;
                mRequest.ContentLength = data.Length;
                mRequest.Timeout = timeOut;
                mRequest.KeepAlive = true;
                mRequest.ProtocolVersion = HttpVersion.Version10;
                //请求流
                var requestStream = mRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
                //响应流
                var mResponse = mRequest.GetResponse() as HttpWebResponse;
                var responseStream = mResponse.GetResponseStream();
                if (responseStream != null)
                {
                    var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                    //获取返回的信息
                    result = streamReader.ReadToEnd();
                    streamReader.Close();
                    responseStream.Close();
                }
            }
            catch (System.Net.WebException ex)
            {
                //响应流
                var mResponse = ex.Response as HttpWebResponse;
                var responseStream = mResponse.GetResponseStream();
                if (responseStream != null)
                {
                    var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                    //获取返回的信息
                    result = streamReader.ReadToEnd();
                    streamReader.Close();
                    responseStream.Close();
                }
                result = "获取数据失败,请重试!" + url + ex.ToString() + "         返回数据" + result;
            }

 

十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误

上一篇:整理:C#常用字符串操作,常用数值类型转换


下一篇:win7 docker安装文件及安装问题