Log on to the C/S application through http to obtain relevant information.

Source: Internet
Author: User
Tags baseuri

Some time ago, when processing a Samsung device and using its library, you must specify the device type when logging on to the device. The related information in the application does not store the device type field, when I don't know the model of this device in the application, I will ask the manufacturer how to solve the problem and reply to me to connect the device types one by one! It's abnormal. Several types of devices have been tested for a long time. Because his devices support the HTTP and RTSP protocols, there is a page on which the device type can be seen during HTTP management. Every device has a web service, they are all the same interfaces. In this way, when I log on first, I make a Web request in the application to obtain the device model first, and then use TCP to connect, this Web Service is basically always open.

According to a description of its HTTP protocol, the web device logon adopts the standard HTTP digest access authentication method of rfc2069 authentication protocol. For more information, see rfc2069, wireshark is used to track the logon process, which is not very standard. Many details are different.

In Delphi, there is a tiddigestauthentication class, which is used to implement this. However, this is a standard, but it is still different from the device. The HTTP request reverse identification is a string with a specific format, so I made a class to overload its authentication method:

Tdigestauth = Class (tiddigestauthentication)
Private
Fwwwauthenticate: string;
Fbaseuri: string;
Public
Constructor create (wwwauthenticate, buri: string );
Function authentication: string; override;
Property wwwauthenticate: String read fwwwauthenticate write fwwwauthenticate;
Property baseuri: String read fbaseuri write fbaseuri;
End;

Just generate a specific request in this method:

Function tdigestauth. Authentication: string;
VaR
Authtext, authorizationstr: string;
A1, A2, method: string;
Realm, nonce, Uri, qop, cnonce, NC, response: string;
IPOs, steps: integer;
Begin
Authtext: = wwwauthenticate;
IPOs: = pos ('nonce = "', authtext );
If IPOs> 0 then
Begin
Delete (authtext, 1, IPOs + 6 );
End;
IPOs: = pos ('"', authtext );
If IPOs> 0 then
Begin
Nonce: = copy (authtext, 1, ipos-1 );
End;
 
Realm: = 'ipolis ';
Qop: = 'auth ';
Uri: = baseuri;

Cnonce: = '01a34a ';
Steps: = 1;
NC: = format ('%. 8d', [steps]);
A1: = md5print (md5string (username + ':' + realm + ':' + password ));
Method: = 'get ';
A2: = md5print (md5string (method + ':' + URI ));
Response: = md5print (md5string (A1 + ':' + nonce + ':' + NC + ':' + cnonce + ':' + qop + ': '+ A2 ));

Authorizationstr: = format ('digest username = "% s", realm = "% s", Nonce = "% s", uri = "% s", '+
'Cnonce = "% s", NC = % s, response = "% s", qop = "% s "',
[Username, realm, nonce, Uri, cnonce, NC, response, qop]);
Result: = authorizationstr;
End;

Note that the preceding A1, A2, and response formats are composed of standard RFC format. See here: http://en.wikipedia.org/wiki/Digest_access_authentication

Or download an rfc2069 document.

The above method returns the HTTP content of the final login request.

In the application, I will write the following procedure to log on to and request the device type:

Function txxxsam. getdevicemodelname: string;
Function fromhtmlresponsegetmodelname (responsetext: string): string;
VaR
TS: tstringlist;
I, IPOs: integer;
Modelstr: string;
Begin
Modelstr: = '';
Result: = '';
Responsetext: = stringreplace (responsetext, #13 #10, '#', [rfreplaceall]);
IPOs: = pos ('model', responsetext );
Modelstr: = copy (responsetext, IPOs, length (responsetext ));
IPOs: = pos ('#', modelstr );
Result: = copy (modelstr, 7, iPos-7 );
End;
VaR
URL, Uri: string;
Idhttp: tidhttp;
Responsestream: tstringstream;
Digestauth: tdigestauth;
Begin
Result: = '';
URL: = format ('HTTP: // % S % s', [IP, samhttpgetparam]);
Uri: = samhttpgetparam;
Idhttp: = tidhttp. Create (NiL );
Responsestream: = tstringstream. Create ('');
Try

Try
Responsestream. Seek (0, 0 );
Idhttp. Get (URL, responsestream );
Except
;
End;
If (idhttp. responsecode = 401) and // 401 request Login
(Pos ('auth ', idhttp. response. wwwauthenticate. Text)> 0) then
Begin
Digestauth: = tdigestauth. Create (idhttp. response. wwwauthenticate. Text, Uri );
Idhttp. Request. Authentication: = digestauth;
Idhttp. Request. Authentication. Username: = self. Username;
Idhttp. Request. Authentication. Password: = self. Password;
Idhttp. Request. setheaders;
Try
Responsestream. Seek (0, 0 );
Idhttp. Get (URL, responsestream );
Except
;
End;

If idhttp. responsecode = 404 then // The requested page does not exist: Samsung has multiple types of HTTP parameters, and some types of addresses are different.
Begin
URL: = format ('HTTP: // % S % s', [IP, samhttpgetparam2]);
Uri: = samhttpgetparam2;
Digestauth. baseuri: = URI;
Idhttp. Request. setheaders;
Try
Responsestream. Seek (0, 0 );
Idhttp. Get (URL, responsestream );
Except
;
End;
End;

If idhttp. responsecode = 200 then // success
Result: = fromhtmlresponsegetmodelname (responsestream. datastring)
Else if idhttp. responsecode = 401 then
Lastmessageinfo: = 'HTTP request logon failed'
Else
Begin
Lastmessageinfo: = 'HTTP request information failed: '+ idhttp. responsetext;
Writelog (samgetlasterror (lastmessageinfo ));
End;
End
Else if idhttp. responsecode = 200 then // success
Begin
Result: = fromhtmlresponsegetmodelname (responsestream. datastring );
End
Else
Begin
Lastmessageinfo: = 'HTTP request information failed: '+ idhttp. responsetext;
Writelog (samgetlasterror (lastmessageinfo ));
End;
Finally
Responsestream. Free;
Idhttp. Free;
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.