不允许使用C#405方法

时间:2011-08-24 22:16:51

标签: c# httpwebrequest http-status-code-405

我将数据发布到网站并显示生成的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)Method Not Allowed。

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

0 个答案:

没有答案