使用Endpoint调用WebRequest会生成591

时间:2018-02-24 05:23:30

标签: c# httpwebrequest

您好我正在尝试调用在我的机器上运行的Web服务,当我在没有端点的情况下调用它时,一切运行正常,当我添加端点时,给我一个由webserver返回的错误591,这是我的代码:< / p>

var request = (HttpWebRequest)WebRequest.Create("http://localhost:8111/fiscal");

        request.Method = "POST";
        request.ContentLength = 0;
        request.ContentType = "application/x-www-form-urlencoded";
        request.Host = "localhost:8111";

        var encoding = new UTF8Encoding();
        var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(JsonConvert.SerializeObject(formData));
        request.ContentLength = bytes.Length;

        using (var writeStream = request.GetRequestStream())
        {
            writeStream.Write(bytes, 0, bytes.Length);
        }


        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                throw new ApplicationException(message);
            }

            // grab the response
            using (var responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseValue = reader.ReadToEnd();
                    }
            }

            MessageBox.Show(responseValue);
        }

0 个答案:

没有答案