在做一个截取网站内容的程序,顺便学学如何用程序与网站通讯,但在实施的过程中碰到了问题:问题描述:网站的页面中有一个form,post方式的,目标指向该网站目录下的一个query.asp文件,如下:<FORM id=form1 name=form1 action=query.asp method=post>提交表单的是一个按钮,如下:<INPUT type=submit value=Search name=B1>提交的内容除了属性B1之外,还有两个下拉框中的属性
<SELECT size=1 name=month>
<SELECT size=1 name=day>通过程序模拟发送时数据包有两个:
0000   00 01 f4 1f 97 29 00 12 3f db 21 eb 08 00 45 00  .....)..?.!...E.
0010   02 54 53 fe 40 00 80 06 1b 35 d2 1d f0 f2 d2 1d  [email protected]......
0020   f4 42 0f 23 27 60 2e 90 09 ec ad b9 98 03 50 18  .B.#'`........P.
0030   fa f0 d0 fe 00 00 50 4f 53 54 20 68 74 74 70 3a  ......POST http:
0040   2f 2f 77 77 77 31 2e 77 73 74 2e 6e 65 74 2e 63  //www1.wst.net.c
0050   6e 2f 73 63 72 69 70 74 73 2f 66 6c 65 78 2f 54  n/scripts/flex/T
0060   6f 64 61 79 4f 6e 48 69 73 74 6f 72 79 2f 71 75  odayOnHistory/qu
0070   65 72 79 2e 61 73 70 20 48 54 54 50 2f 31 2e 31  ery.asp HTTP/1.1
0080   0d 0a 41 63 63 65 70 74 3a 20 69 6d 61 67 65 2f  ..Accept: image/
0090   67 69 66 2c 20 69 6d 61 67 65 2f 78 2d 78 62 69  gif, image/x-xbi
00a0   74 6d 61 70 2c 20 69 6d 61 67 65 2f 6a 70 65 67  tmap, image/jpeg
00b0   20 20 2c 20 69 6d 61 67 65 2f 70 6a 70 65 67 2c    , image/pjpeg,
00c0   20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 73   application/x-s
00d0   68 6f 63 6b 77 61 76 65 2d 66 6c 61 73 68 2c 20  hockwave-flash, 
00e0   61 70 70 6c 69 63 61 74 69 6f 6e 2f 76 6e 64 2e  application/vnd.
00f0   6d 73 2d 65 78 63 65 6c 2c 20 61 70 70 6c 69 63  ms-excel, applic
0100   61 74 69 6f 6e 2f 76 6e 64 2e 6d 73 2d 70 6f 77  ation/vnd.ms-pow
0110   65 72 70 6f 69 6e 74 2c 20 61 70 70 6c 69 63 61  erpoint, applica
0120   74 69 6f 6e 2f 6d 73 77 6f 72 64 2c 20 2a 2f 2a  tion/msword, */*
0130   0d 0a 52 65 66 65 72 65 72 3a 20 68 74 74 70 3a  ..Referer: http:
0140   2f 2f 77 77 77 31 2e 77 73 74 2e 6e 65 74 2e 63  //www1.wst.net.c
0150   6e 2f 73 63 72 69 70 74 73 2f 66 6c 65 78 2f 54  n/scripts/flex/T
0160   6f 64 61 79 4f 6e 48 69 73 74 6f 72 79 2f 71 75  odayOnHistory/qu
0170   65 72 79 2e 61 73 70 3b 0d 0a 55 73 65 72 2d 41  ery.asp;..User-A
0180   67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 28 63 6f 6d  gent: Mozill(com
0190   70 61 74 69 62 6c 65 3b 20 4d 53 49 45 20 36 2e  patible; MSIE 6.
01a0   30 3b 20 57 69 6e 64 6f 77 73 20 4e 54 20 35 2e  0; Windows NT 5.
01b0   31 3b 20 4d 61 78 74 68 6f 6e 3b 20 2e 4e 45 54  1; Maxthon; .NET
01c0   20 43 4c 52 20 31 2e 31 2e 34 33 32 32 29 0d 0a   CLR 1.1.4322)..
01d0   43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70  Content-Type: ap
01e0   70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d  plication/x-www-
01f0   66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 0d  form-urlencoded.
0200   0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a  .Content-Length:
0210   20 32 35 0d 0a 45 78 70 65 63 74 3a 20 31 30 30   25..Expect: 100
0220   2d 63 6f 6e 74 69 6e 75 65 0d 0a 50 72 6f 78 79  -continue..Proxy
0230   2d 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65  -Connection: Kee
0240   70 2d 41 6c 69 76 65 0d 0a 48 6f 73 74 3a 20 77  p-Alive..Host: w
0250   77 77 31 2e 77 73 74 2e 6e 65 74 2e 63 6e 0d 0a  ww1.wst.net.cn..
0260   0d 0a                                            ..
0000   00 01 f4 1f 97 29 00 12 3f db 21 eb 08 00 45 00  .....)..?.!...E.
0010   00 41 53 ff 40 00 80 06 1d 47 d2 1d f0 f2 d2 1d  [email protected]......
0020   f4 42 0f 23 27 60 2e 90 0c 18 ad b9 98 03 50 18  .B.#'`........P.
0030   fa f0 78 75 00 00 26 6d 6f 6e 74 68 3d 31 26 64  ..xu..&month=1&d
0040   61 79 3d 31 30 26 42 31 3d 53 65 61 72 63 68     ay=10&B1=Search然后返回一个HTTP 500 错误的数据包
而正确的发送如下形式的数据包,就能获得所要的结果:
0000   00 01 f4 1f 97 29 00 12 3f db 21 eb 08 00 45 00  .....)..?.!...E.
0010   02 b3 54 2b 40 00 80 06 1a a9 d2 1d f0 f2 d2 1d  ..T+@...........
0020   f4 42 0f 27 27 60 31 b0 33 67 ba 2f 53 3c 50 18  .B.''`1.3g./S<P.
0030   fa f0 b2 e6 00 00 50 4f 53 54 20 68 74 74 70 3a  ......POST http:
0040   2f 2f 77 77 77 31 2e 77 73 74 2e 6e 65 74 2e 63  //www1.wst.net.c
0050   6e 2f 73 63 72 69 70 74 73 2f 66 6c 65 78 2f 54  n/scripts/flex/T
0060   6f 64 61 79 4f 6e 48 69 73 74 6f 72 79 2f 71 75  odayOnHistory/qu
0070   65 72 79 2e 61 73 70 20 48 54 54 50 2f 31 2e 30  ery.asp HTTP/1.0
0080   0d 0a 41 63 63 65 70 74 3a 20 69 6d 61 67 65 2f  ..Accept: image/
0090   67 69 66 2c 20 69 6d 61 67 65 2f 78 2d 78 62 69  gif, image/x-xbi
00a0   74 6d 61 70 2c 20 69 6d 61 67 65 2f 6a 70 65 67  tmap, image/jpeg
00b0   2c 20 69 6d 61 67 65 2f 70 6a 70 65 67 2c 20 61  , image/pjpeg, a
00c0   70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 73 68 6f  pplication/x-sho
00d0   63 6b 77 61 76 65 2d 66 6c 61 73 68 2c 20 61 70  ckwave-flash, ap
00e0   70 6c 69 63 61 74 69 6f 6e 2f 76 6e 64 2e 6d 73  plication/vnd.ms
00f0   2d 65 78 63 65 6c 2c 20 61 70 70 6c 69 63 61 74  -excel, applicat
0100   69 6f 6e 2f 76 6e 64 2e 6d 73 2d 70 6f 77 65 72  ion/vnd.ms-power
0110   70 6f 69 6e 74 2c 20 61 70 70 6c 69 63 61 74 69  point, applicati
0120   6f 6e 2f 6d 73 77 6f 72 64 2c 20 2a 2f 2a 0d 0a  on/msword, */*..
0130   52 65 66 65 72 65 72 3a 20 68 74 74 70 3a 2f 2f  Referer: http://
0140   77 77 77 31 2e 77 73 74 2e 6e 65 74 2e 63 6e 2f  www1.wst.net.cn/
0150   73 63 72 69 70 74 73 2f 66 6c 65 78 2f 54 6f 64  scripts/flex/Tod
0160   61 79 4f 6e 48 69 73 74 6f 72 79 2f 0d 0a 41 63  ayOnHistory/..Ac
0170   63 65 70 74 2d 4c 61 6e 67 75 61 67 65 3a 20 7a  cept-Language: z
0180   68 2d 63 6e 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79  h-cn..Content-Ty
0190   70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f  pe: application/
01a0   78 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e  x-www-form-urlen
01b0   63 6f 64 65 64 0d 0a 50 72 6f 78 79 2d 43 6f 6e  coded..Proxy-Con
01c0   6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c  nection: Keep-Al
01d0   69 76 65 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a  ive..User-Agent:
01e0   20 4d 6f 7a 69 6c 6c 61 2f 34 2e 30 20 28 63 6f   Mozilla/4.0 (co
01f0   6d 70 61 74 69 62 6c 65 3b 20 4d 53 49 45 20 36  mpatible; MSIE 6
0200   2e 30 3b 20 57 69 6e 64 6f 77 73 20 4e 54 20 35  .0; Windows NT 5
0210   2e 31 3b 20 4d 61 78 74 68 6f 6e 3b 20 2e 4e 45  .1; Maxthon; .NE
0220   54 20 43 4c 52 20 31 2e 31 2e 34 33 32 32 29 0d  T CLR 1.1.4322).
0230   0a 48 6f 73 74 3a 20 77 77 77 31 2e 77 73 74 2e  .Host: www1.wst.
0240   6e 65 74 2e 63 6e 0d 0a 43 6f 6e 74 65 6e 74 2d  net.cn..Content-
0250   4c 65 6e 67 74 68 3a 20 32 34 0d 0a 50 72 61 67  Length: 24..Prag
0260   6d 61 3a 20 6e 6f 2d 63 61 63 68 65 0d 0a 43 6f  ma: no-cache..Co
0270   6f 6b 69 65 3a 20 41 53 50 53 45 53 53 49 4f 4e  okie: ASPSESSION
0280   49 44 41 51 44 41 53 53 53 53 3d 42 43 4c 4d 46  IDAQDASSSS=BCLMF
0290   43 43 44 4a 4f 48 4c 49 4f 4e 41 4b 4b 47 46 4c  CCDJOHLIONAKKGFL
02a0   48 48 46 0d 0a 0d 0a 6d 6f 6e 74 68 3d 31 26 64  HHF....month=1&d
02b0   61 79 3d 31 30 26 42 31 3d 53 65 61 72 63 68 0d  ay=10&B1=Search.
02c0   0a                                               .
我的问题是如何将前两个数据包合并成一个,就像最后那个一样,注意到最后一个数据包已经将头部和查询字符串整合到一起了,望指点

解决方案 »

  1.   

    一个包和两个包效果是一样的,500错误应该不是这个原因导致的,你发的那个Content-Length为什么是25?
      

  2.   

    不太清楚你是用什么方式发的,使用WebRequest对象处理POST提交很容易,以下是个例子.
    using System;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Web;class ClientPOST {
        public static void Main(string[] args) {        if (args.Length < 1) {
                showusage();
            } else {
                if (args.Length < 2 ) {
                    getPage(args[0], "s1=food&s2=bart");
                } else {
                    getPage(args[0], args[1]);
                }
            }        Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();        return;
        }    public static void showusage() {
            Console.WriteLine("Attempts to POST into to a URL");
            Console.WriteLine();
            Console.WriteLine("Usage:");
            Console.WriteLine("ClientPOST URL [postdata]");
            Console.WriteLine();
            Console.WriteLine("Examples:");
            Console.WriteLine("ClientPOST http://www.microsoft.com s1=food&s2=bart");
        }    public static void getPage(String url, String payload) {
            WebResponse result = null;        try {            WebRequest req = WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
            StringBuilder UrlEncoded = new StringBuilder();
            Char[] reserved = {'?', '=', '&'};
            byte[] SomeBytes = null;        if (payload != null) {
            int i=0, j;
            while(i<payload.Length){
                j=payload.IndexOfAny(reserved, i);
                if (j==-1){
                UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i)));
                break;
                }
                UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
                UrlEncoded.Append(payload.Substring(j,1));
                i = j+1;
            }
            SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
            newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                } else {
                    req.ContentLength = 0;
                }
                result = req.GetResponse();
                Stream ReceiveStream = result.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader sr = new StreamReader( ReceiveStream, encode );
                Console.WriteLine("\r\nResponse stream received");
                Char[] read = new Char[256];
                int count = sr.Read( read, 0, 256 );
                Console.WriteLine("HTML...\r\n");
                while (count > 0) {
                    String str = new String(read, 0, count);
                    Console.Write(str);
                    count = sr.Read(read, 0, 256);
                }
                Console.WriteLine("");
            } catch(Exception e) {
                Console.WriteLine( e.ToString());
                Console.WriteLine("\r\nThe request URI could not be found or was malformed");
            } finally {
                if ( result != null ) {
                    result.Close();
                }
            }
        }
    }