尝试从URL获取授权令牌可以在浏览器中使用,但不能在WebRequest中使用

时间:2017-12-26 20:29:47

标签: c# webrequest

我正在努力设置授权的休息请求,而且我有一段时间可以获得有效的回复。如果我将请求URL粘贴到浏览器(Firefox Quantum和Chrome)中,我可以获得状态:已验证;令牌:[令牌字符串]的响应,但是当我尝试使用WebRequest.Create([url])时,我会得到&的响应#34; 400:错误请求"。我直接从调试代码复制URL,所以我知道它是有效的。我很确定我做了一些简单的错误。有人会指出我正确的方向吗?

            string loginReq = _authPath + "?user=" + _username + "&pw=" + _password;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginReq);
        request.Accept = "text/html, application/xhtml + xml, */*";
        request.UserAgent = "Mozilla/5.0(Windows NT 6.1; WOW64; Trident/7.0; rv: 11.0) like Gecko";
        request.KeepAlive = true;



        WebResponse response = request.GetResponse();
        Console.WriteLine(response.ResponseUri);

        Console.Read();

好的,在做了一些更多的戳之后,看起来我打电话的网站拒绝了请求,因为它认为我使用的是IE9。这是代码的最新版本             私有静态字符串GetAuthorization(){             string token = string.Empty;

        string loginReq =  _authPath + "?user=" + _ftpUsername + "&pw=" + _ftpPassword;

        string task = SendWebRequest(loginReq);
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginReq);
            //request.Accept = "text/html, application/xhtml + xml, */*";
            //request.UserAgent = "Mozilla/5.0(Windows NT 6.1; WOW64; Trident/7.0; rv: 11.0) like Gecko";
            //request.KeepAlive = true;
            //request.Headers.Add("Accept-Encoding", "gzip, deflate");
            //request.Headers.Add("Cache-Control", "no-cache");
            //request.Headers.Add("Accept-Language", "en-US");
            //request.Headers.Add("DNT", "1");
            //request.Method = "GET";
            //request.CookieContainer = new CookieContainer();
            //request.Headers.Add("Request", "GET /xc2/QAPI_Upload?user=user@ottrtest1.com&pw=ETqDJeQ1! HTTP/1.1");


            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(task);

        Console.Read();

        return token;
    }

    public static string SendWebRequest(string requestUrl) {
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = client.GetAsync(requestUrl).GetAwaiter().GetResult())
            return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    }

我一直在尝试不同的事情,但我得到了相同的结果(400次错误请求)这是我与之合作的最新版本

        string loginReq = $"{_authPath}?user={_userName}&pw={_passWord}";
        string result;
        using (WebClient wc = new WebClient()) {
            var json = wc.DownloadString(loginReq);
            result = json.ToString();
            Console.WriteLine(json);
        }

如果我将网址更改为" https://www.google.com"我的代码有效。如果我将loginReq粘贴到SoapUI它可以工作,我不能让网址和我的代码一起工作......

Fiddler发现了这个问题。一旦我在fiddler中查看了请求,我就看到我需要将安全协议类型设置为tls1.0,tls1.1或tls1.2。一旦我这样做,我终于得到了工作的号召。这是工作代码,以防任何人需要它作为参考:

            ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
        string loginReq = $"{_authPath}?user={_userName}&pw={_passWord}";
        string result;
        using (WebClient wc = new WebClient()) {
            var json = wc.DownloadString(loginReq);
            result = json.ToString();
            Console.WriteLine(json);
        }


        return result;

1 个答案:

答案 0 :(得分:0)

Fiddler发现了这个问题。一旦我在fiddler中查看了请求,我就看到我需要将安全协议类型设置为tls1.0,tls1.1或tls1.2。一旦我这样做,我终于得到了工作的号召。这是工作代码,以防任何人需要它作为参考:

        ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    string loginReq = $"{_authPath}?user={_userName}&pw={_passWord}";
    string result;
    using (WebClient wc = new WebClient()) {
        var json = wc.DownloadString(loginReq);
        result = json.ToString();
        Console.WriteLine(json);
    }


    return result;