开发者

C# 405 methods not allowed

开发者 https://www.devze.com 2023-03-30 18:58 出处:网络
I am posting data to a website and displaying the resulting json data, This is the function I ended up with after taking some pieces from msdn\'s example

I am posting data to a website and displaying the resulting json data, This is the function I ended up with after taking some pieces from msdn's example

        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.Ge开发者_如何转开发tResponseStream();

                reader = new StreamReader(dataStream);

                responseFromServer = reader.ReadToEnd();

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


            return responseFromServer;
        }
    }

The ip and port variables are within the class that this function is inside of. When GetResponse() is called, it this exception.

{System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.

Ive tested it and it is only because of the proxy, but the parameters are correct. Why is this exception being thrown?

0

精彩评论

暂无评论...
验证码 换一张
取 消