远程服务器返回错误:(405)mvc 5中不允许的方法

时间:2016-12-29 06:22:59

标签: c# http asp.net-mvc-5

我使用与我们用于gmail登录时相同的凭据来登录我的应用程序。          我收到远程服务器返回错误:

  

(405)方法不允许,

on

 var httpResponse = (HttpWebResponse)request.GetResponse();

以下是我的代码示例:

        var stringBuilder = new StringBuilder();
        var request = (HttpWebRequest)WebRequest.Create(url);
        var captcha = model.catchpa;
        var username = model.Email;
        var user = "UserName";
        var password = model.Password;
        var data = Encoding.ASCII.GetBytes(string.Format("username={0}&user={1}&password={2}&captcha={3}", username, user, password, captcha));
        request.ContentLength = data.Length;
        request.Method = "POST";
        request.ContentType = "application/json;charset=UTF-8";
        request.Accept = "*";
        request.Headers.Add("Accept-Encoding", " gzip, deflate, br");
        request.Headers.Add("Accept-Language", "en-GB,en;q=0.5");
        request.Headers.Add("Cache-Control", "max-age=0");
        request.Headers.Add("Upgrade-Insecure-Requests", "1");
        request.Headers.Add("Cookie", "x-apm-brtm-bt-p=Chrome; x-apm-brtm-bt-pv=55; x-apm-brtm-bt-url=%2F; WMRUMC=%7B%7D; CaptchaCookie=6cd0895847f3412e842dff4d932543e6; x-apm-brtm-response-bt=bs%3D-1%2Cbt%3D-1%2Cbtc%3D-1%2CCorBrowsGUID%3D4521608D0AF27A07195F9576C4193CD6%2Curl%3D%2Fservices%2Fcaptcha; x-apm-brtm-response-bt-id=_oxn3avih0");
        request.KeepAlive = true;
        request.Host = "services.gst.gov.in";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
        request.Referer = "https://services.gst.gov.in/services/login";

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var httpResponse = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

request.Method = "POST";

您正在使用HTTP POST获取数据。服务器不接受您的发布请求,因为根本不允许POST或您的请求不完整。

使用 Fiddler 之类的HTTP嗅探工具,并在网络浏览器中手动执行请求。在那里,您可以看到如何正确访问该页面。