想尝试模拟查询自己的档案是否已经邮寄到,结果失败了

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/7/3
 * 时间: 8:04
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using System.Net;
using System.IO;
using HtmlAgilityPack;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Text;
namespace DYHR
{
    class Program
    {
        static CookieContainer GetCookieContainer()
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            CookieContainer cc = new CookieContainer();
            try
            {
                request = (HttpWebRequest)WebRequest.Create("http://www.dyhr.cn/daSearch.aspx");
                request.Method = "POST";
                request.Referer = "http://www.dyhr.cn/daSearch.aspx";
                request.ContentType = "application/x-www-form-urlencoded";
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                string postData = "txtName="+HttpUtility.UrlEncode("常伟华", Encoding.UTF8)+"&txtDate="+HttpUtility.UrlEncode("1991-2-9", Encoding.UTF8);
                byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(postData);
                request.ContentLength = postDataBytes.Length;
                   
                Stream stream = request.GetRequestStream();
                stream.Write(postDataBytes, 0, postDataBytes.Length);
                stream.Close();
                   
                response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                string data = reader.ReadToEnd();
                Console.WriteLine(data);
                reader.Close();
                Console.WriteLine("Cookies");
                foreach(Cookie cookie in response.Cookies)
                {
                    Console.WriteLine("{0}={1}", cookie.Name, cookie.Value);
                    cc.Add(cookie);
                }
                request.Abort();
                response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return cc;
        }
           
        public static void Main(string[] args)
        {
            GetCookieContainer();
               
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            CookieContainer cc = null;
               
            try
            {
                cc = GetCookieContainer();
                request = (HttpWebRequest)WebRequest.Create("http://www.dyhr.cn/daSearch.aspx");
                request.Method = "POST";
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Referer = "http://www.dyhr.cn/daSearch.aspx";
                string postData = "txtName="+HttpUtility.UrlEncode("常伟华", Encoding.UTF8)+"&txtDate="+HttpUtility.UrlEncode("1991-2-9", Encoding.UTF8);
                byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(postData);
                request.ContentLength = postDataBytes.Length;
                request.KeepAlive = true;
                Console.WriteLine("ContentLength = {0}", postDataBytes.Length);
                   
                request.CookieContainer = cc;
                   
                Stream stream = request.GetRequestStream();
                stream.Write(postDataBytes, 0, postDataBytes.Length);
                stream.Close();
                   
                response = (HttpWebResponse)request.GetResponse();
                   
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                string html = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                request.Abort();
                response.Close();
                   
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                   
                HtmlNode node = doc.GetElementbyId("lblBz");
                   
                Console.WriteLine(node.OuterHtml);
                   
                   
                   
                   
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
               
            Console.ReadKey(true);
        }
    }
}


知识共享许可协议
《想尝试模拟查询自己的档案是否已经邮寄到,结果失败了》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

1 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     167.97ms