c# HttpClient身份验证不工作(AXIS Cam cgi API)

本文关键字:Cam cgi API AXIS HttpClient 身份验证 工作 | 更新日期: 2023-09-27 18:11:26

我尝试了很多不同的方法来验证,但没有一个工作,要么程序崩溃,要么我没有得到响应:

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)在我的web浏览器有和没有密码的用户名:

(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# HttpClient身份验证不工作(AXIS Cam cgi API)

您可以这样做:

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中的参数useUnsafeHeaderParsing。配置可能有用:

<system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
</system.net>