採集/自動登録はこの二つの方法でasp.netを実現できます。

7581 ワード

/// <summary>
///   get    xmlHttp  ,       
/// </summary>
/// <param name="Url">URL  ,        , :http://www.baidu.com/index.asp?id=7</param>
/// <param name="Encoding">              ,  "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithGet(string Url, string Encoding, CookieContainer cookie)
{
    HttpWebRequest request;// = (HttpWebRequest)WebRequest.Create(Url);
    string ResponseHtml = "";
    try
    {
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.CookieContainer = cookie;
        request.KeepAlive = false; //        
        request.Timeout = 6000; //    
        request.Method = "get"; //get    
        request.ContentType = "application/x-www-form-urlencoded;charset=" + Encoding;
        request.AllowAutoRedirect = true; //       
        request.MaximumAutomaticRedirections = 10; //      
        request.AllowWriteStreamBuffering = false; //             
        request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; //      

        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //    
        Stream responseStream = response.GetResponseStream(); //     
        StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(Encoding)); //          

        ResponseHtml = readStream.ReadToEnd(); //   
        responseStream.Close(); //     
        readStream.Close();//     
    }
    catch (Exception ex)
    {
        ResponseHtml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"    !" + ex.Message.ToString() + "\" /></data>";
    }
    finally { }
    return ResponseHtml;
}

/// <summary>
///   post    xmlHttp  ,       
/// </summary>
/// <param name="url">URL  </param>
/// <param name="parms">          </param>
/// <param name="encoding">              ,  "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithPost(string url, Hashtable parms, string encoding, CookieContainer cookie)
{
    HttpWebRequest request ;//= (HttpWebRequest)WebRequest.Create(url);
    string ResponseHtml = "";
    try
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookie;
        ////   
        if (encoding == null) { encoding = "utf-8"; }
        Encoding myEncoding = Encoding.GetEncoding(encoding); //    

        string parmsStr = "";
        if (parms != null)
        {
            foreach (DictionaryEntry item in parms)
            {
                parmsStr += "&" + HttpUtility.UrlEncode(item.Key.ToString(), myEncoding);
                parmsStr += "=" + HttpUtility.UrlEncode(item.Value.ToString(), myEncoding);
            }
            if (parmsStr.Length > 0)
            {
                parmsStr = parmsStr.Substring(1, parmsStr.Length - 1); //    "&"  
            }
        }
        byte[] postBytes = myEncoding.GetBytes(parmsStr);

        request.Timeout = 60000; //    
        request.Method = "post"; //  post    
        request.ContentType = "application/x-www-form-urlencoded;charset=" + myEncoding;
        request.ContentLength = postBytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postBytes, 0, postBytes.Length); //        
        requestStream.Close();

        //    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();//    
        Stream responseStream = response.GetResponseStream();//     
        StreamReader readStream = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); //  StreamReader          

        ResponseHtml = readStream.ReadToEnd(); //   
        responseStream.Close(); //     
        readStream.Close();//     
    }
    catch (Exception ex) 
    {
        ResponseHtml= "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"    !" + ex.Message.ToString() + "\" /></data>";
    }
    return ResponseHtml;