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

问题描述 投票:0回答:1

我正在努力建立一个授权的休息请求,我有一段时间得到有效的回复。如果我将请求URL粘贴到浏览器(Firefox Quantum和Chrome)中,我可以获得Status:Authenticated; token:[token string]的响应,但是当我尝试WebRequest.Create([url])时,我会得到“ 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/[email protected]&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 Bad Request)这是我正在使用的最新版本

        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中查看了请求,我就看到我需要将安全协议类型设置为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;
c# webrequest
1个回答
0
投票

提琴手发现了这个问题。一旦我在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;
© www.soinside.com 2019 - 2024. All rights reserved.