C# HttpClient 身份验证不起作用(AXIS Cam cgi API)

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

我尝试了很多不同的方法来进行身份验证,但没有一个起作用,要么程序崩溃,要么我没有得到任何响应:

async void GetRequest(string url)
    {
        //Test 1
        /*var credentials = new NetworkCredential("root", "root");
        var handler = new HttpClientHandler { Credentials = credentials };*/

        //Test 2
        /*var credCache = new CredentialCache();
        credCache.Add(new Uri(url), "Digest", new NetworkCredential("root", "root"));
        var handler = new HttpClientHandler { Credentials = credCache };*/

        using (HttpClient client = new HttpClient(/*handler*/))
        {
            //Test 3
            /*var encoding = new ASCIIEncoding();
            var authHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoding.GetBytes(string.Format("{0}:{1}", "root", "root"))));
            client.DefaultRequestHeaders.Authorization = authHeader;*/

            //Test 4 & 5
            /*var headerVal = Convert.ToBase64String(Encoding.ASCII.GetBytes("root:root"));
            var headerVal = Convert.ToBase64String(Encoding.UTF8.GetBytes("root:root"));
            var header = new AuthenticationHeaderValue("Basic", headerVal);
            client.DefaultRequestHeaders.Authorization = header;*/

            using (HttpResponseMessage response = await client.GetAsync(url))
            {
                using (HttpContent content = response.Content)
                {
                    string mycontent = await content.ReadAsStringAsync();
                    Debug.WriteLine(mycontent);
                }
            }
        }
    }

当我在网络浏览器中尝试使用或不使用密码用户名的链接(http://192.168.68.127/axis-cgi/param.cgi)时,以下是一些请求标头和响应:

(Windows 安全

服务器 192.168.68.127 正在询问您的用户名和密码。服务器报告它来自 AXIS_00408C9A1F56。)

没有:

HTTP/1.1 401 Unauthorized
Date: Mon, 03 Oct 2016 07:31:44 GMT
Accept-Ranges: bytes
Connection: close
WWW-Authenticate: Digest realm = "AXIS_00408C9A1F56",
nonce = "0010e555Y3035383167c1586f9d3cbd5827c202d485cb9", stale = FALSE,
qop = "auth"
Basic realm = "AXIS_00408C9A1F56"
Content-Length: 184
Content-Type: text/html; charset=ISO-8859-1


GET /axis-cgi/param.cgi HTTP/1.1
Host: 192.168.68.127
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 
Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1


<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY><H1>401 Unauthorized</H1>
Your client does not have permission to get URL /axis-cgi/param.cgi from 
this server.
</BODY></HTML>

与:

HTTP/1.1 200 OK
Date: Mon, 03 Oct 2016 07:29:46 GMT
Accept-Ranges: bytes
Connection: close
Authentication-Info: qop=auth, rspauth="aa088ab10537d01e434856d0bab92930", 
cnonce="1a005dc72160cede", nc=00000002
Content-Type: text/plain


GET /axis-cgi/param.cgi HTTP/1.1
Host: 192.168.68.127
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 
Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Authorization: Digest username="root", realm="AXIS_00408C9A1F56", 
nonce="0010e4b2Y82490734fa5d5f4f6ccc48546da4c59d6fc85", uri="/axis-
cgi/param.cgi", response="dfb04200dafbce37994999dd219c5dd8", qop=auth, 
nc=00000002, cnonce="1a005dc72160cede"
Connection: keep-alive
Upgrade-Insecure-Requests: 1


root.MediaClip.MaxGroups=10
root.MediaClip.M0.Name=Burglar_Alarm_Short
root.MediaClip.M0.Location=/etc/audioclips/Burglar_Alarm_Short_8bit_32kHz_mono_PCM-16bit-little.wav
root.MediaClip.M0.Type=audio
root.MediaClip.M6.Name=Camera clicks
root.MediaClip.M6.Location=/etc/audioclips/camera_clicks16k.au
root.MediaClip.M6.Type=audio
root.MediaClip.M7.Name=Pssst, Psst!
root.MediaClip.M7.Location=/etc/audioclips/pssst_psst16k.au
root.MediaClip.M7.Type=audio
root.MediaClip.M8.Name=Intruder
root.MediaClip.M8.Location=/etc/audioclips/intruder16k.au
root.MediaClip.M8.Type=audio
root.MediaClip.M9.Name=Dog barking
root.MediaClip.M9.Location=/etc/audioclips/dog_barking16k.au
root.MediaClip.M9.Type=audio
c# authentication axis dotnet-httpclient
1个回答
2
投票

您可以执行以下操作:

var uri = new UriBuilder("http://192.168.68.127")
{
     Path = "axis-cgi/param.cgi",
     Query = "action=list"
};
var request = (HttpWebRequest)WebRequest.Create(uri.Uri);
request.Credentials = new NetworkCredential("root", "root");
using (var response = await request.GetResponseAsync())
{
}

我必须提到,对于我拥有的一些相机和一些请求,相机端存在 CRLF 问题,因此 app.config 或 web.config 中的参数

useUnsafeHeaderParsing
可能有用:

<system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>
© www.soinside.com 2019 - 2024. All rights reserved.