Document directory
Myhttp example
The best way to learn any language or tag is to read its example. The sample prototype here comes from the http://www.w3.org/Protocols/HTTP-NG/asn1.html
,
It is a simplified form of fhttp GET requests.
For our example, suppose we need to apply the following ASN.1 ModuleMyhttp. asn
:
MyHTTP DEFINITIONSAUTOMATIC TAGS ::=BEGIN GetRequest ::= SEQUENCE { header-only BOOLEAN, lock BOOLEAN, accept-types AcceptTypes, url Url, ..., timestamp GeneralizedTime } AcceptTypes ::= SET { standards BIT STRING { html(0), plain-text(1), gif(2), jpeg(3) } (SIZE(4)) OPTIONAL, others SEQUENCE OF VisibleString (SIZE(4)) OPTIONAL } Url ::= VisibleString (FROM("a".."z"|"A".."Z"|"0".."9"|"./-_~%#")) v GetRequest ::= {header-only TRUE,lock FALSE,accept-types {standards { html, plain-text }},url "www.asnlab.com",timestamp "20100202020202Z" } END
1. SelectFile
>New
>Project...
2. InASN.1
Category, selectASN.1 Project
And then clickNext
.
3. Enter 'myhttp' in the project name and clickFinish
.
4. Now the ASN.1 file can be added to the project, copied from other places to the source directory of the project, or created from scratch.
5. SelectFile
>New
>Other...
6. InASN.1
Category, selectASN.1 Module
And then clickNext
.
7. Enter 'myhttp' in the module name and clickFinish
.
8. Enter the above source code in the enabled editor.
9. Save the ASN.1 file and compile the Java class file.
Will be generated automatically.
10. Create a Java ProjectTest
And then add the ASN.1 Java Runtime Library.
To the compilation path.
11. Copy the generated Java class file to the appropriate package of the project (in this example, "myhttp") directory.
12. Create a class in the projectTestmyhttp. Java
, Enter the following content:
import java.io.ByteArrayOutputStream;import java.util.Calendar;import MyHTTP.AcceptTypes;import MyHTTP.GetRequest;import MyHTTP.Standards;public class TestMyHttp {public static void main(String[] args) throws Exception {GetRequest getRequest=new GetRequest();getRequest.header_only=true;getRequest.lock=false;getRequest.accept_types=new AcceptTypes();getRequest.accept_types.standards=new Standards(new byte[1],(byte)4);getRequest.accept_types.standards.setHtml();getRequest.accept_types.standards.setPlain_text();getRequest.url="www.asnlab.com";Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, 2010);cal.set(Calendar.MONTH, Calendar.FEBRUARY);cal.set(Calendar.DAY_OF_MONTH, 2);cal.set(Calendar.HOUR_OF_DAY, 2);cal.set(Calendar.MINUTE, 2);cal.set(Calendar.SECOND, 2);cal.set(Calendar.MILLISECOND, 0);cal.set(Calendar.ZONE_OFFSET, 0);getRequest.timestamp = cal.getTime();ByteArrayOutputStream bos=new ByteArrayOutputStream();getRequest.ber_encode(bos);byte[] bs=bos.toByteArray();for(int i=0; i<bs.length; i++) {System.out.printf("%02X ", bs[i] & 0xFF);}}}
13. compile the project. IfAutomatic compilation
Disabled.
14. Right-click the fileTestmyhttp. Java
Then selectRun
>Java application
:
30 2D 80 01 FF 81 01 00 A2 04 80 02 04 C0 83 0e 77 77 2E 61 73 6e 6C 61 62 2E 63 6f 6D 84 0f 32 30 31 30 32 30 31 34 30 32 30 32 5A
Here we will explain in detail the meaning of byte-by-bit after BER encoding:
0x30 -- [0011 | 0000], [Universal, constructed, 16 (sequence)]-getrequest
0x2d -- [0010 | 1101], length 45
0x80 -- [1000 | 0000], [context, primitive, 0 (Boolean)] getrequest. header_only
0x01 -- [0000 | 0001], length 1
0xff -- [0000 | 1111], value true
0x81 -- [1000 | 0001], [context, primitive, 1 (Boolean)] getrequest. Lock
0x01 -- [0000 | 0001], length 1
0x00 -- [0000 | 0000], value false
0xa2 -- [1010 | 0010], [context, constructed, 2 (SET)]-getrequest. accept_types
0x04 -- [0000 | 0100], length 4
0x80 -- [1000 | 0000], [context, primitive, 0 (Bit String)] accepttypes. Standards
0x02 -- [0000 | 0010], length 2
0x04 -- [0000 | 0100], 4 unused bits
0xc0 -- [1100 | 0000], {HTML, plaint_text}
0x83 -- [1000 | 0011], [context, primitive, 3 (visiblestring)] getrequest. url
0x0e -- [0000 | 1100], length 14
0x77 0x77 0x77 0x2e 0x61 0x73 0x6e 0x6c 0x61 0x62 0x2e 0x63 0x6f 0x6d -- www.asnlab.com
0x84 -- [1000 | 0011], [context, primitive, 4 (generalizedtime)] getrequest. Timestamp
0x0f -- [0000 | 1100], length 15
0x32 0x30 0x31 0x30 0x30 0x32 0x30 0x32 0x31 0x34 0x30 0x32 0x30 0 0 0x32 0x5a -- 20100202020202z