C# 405 methods not allowed
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?
精彩评论