0

我正在将数据发布到网站并显示生成的 json 数据,这是我从 msdn 的示例中获取一些片段后最终得到的函数

        private string request(string url, string data)
        {
            byte[] byteArray;

            string postData,
                    responseFromServer;

            WebResponse response;

            Stream dataStream;

            StreamReader reader;

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Proxy = new WebProxy(ip, port);

            responseFromServer = string.Empty;
            request.Method = "POST";
            request.Timeout = 10000;

            postData = data;
            byteArray = Encoding.UTF8.GetBytes(postData);

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;

            try
            {
                dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                response = request.GetResponse();
                dataStream = response.GetResponseStream();

                reader = new StreamReader(dataStream);

                responseFromServer = reader.ReadToEnd();

                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception x)
            {
                x.ToString();
            }


            return responseFromServer;
        }
    }

ip 和 port 变量在这个函数所在的类中。调用 GetResponse() 时,会出现此异常。

{System.Net.WebException:远程服务器返回错误:(405)方法不允许。

我已经测试过了,只是因为代理的原因,但是参数是正确的。为什么会抛出这个异常?

4

0 回答 0