HttpWebRequest的个人见解

  • A+
所属分类:.NET技术
摘要

  

 class ITInfo_SFC     {         public bool bPingFaile = false;         HttpWebRequest http;         public Server server;         public string url = "https://www.baidu.com/";         public string SFC_ip = "127.0.0.1";                            public static bool OnLine()         {             //public string SFC_ip ="127.0.0.1";             Ping ping = new Ping();             PingReply pingReply = ping.Send("127.0.0.1");             if (pingReply.Status == IPStatus.Success)             {                 return true;             }             else                 return false;         }         public void InitURLValue(string url)         {             this.url = url;             http = WebRequest.CreateHttp(url) as HttpWebRequest;          }           public static string PostToUrl(string requestUrl, byte[] byteArrayPost, Encoding encoding)         {             if (OnLine())             {                 try                 {                     string stringResponse = "";                     HttpWebRequest webRequest = WebRequest.CreateHttp(url) as HttpWebRequest;                     webRequest.Method = "POST";                     webRequest.ContentType = "application/x-www-form-urlencoded";                     webRequest.ContentLength = byteArrayPost.Length;                     webRequest.CookieContainer = new CookieContainer();                     webRequest.Credentials = CredentialCache.DefaultCredentials;                      Stream newStream = webRequest.GetRequestStream();                     //写入参数                     newStream.Write(byteArrayPost, 0, byteArrayPost.Length);                     newStream.Close();                       WebResponse webResponse = webRequest.GetResponse();                     StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), encoding);                     stringResponse = responseStream.ReadToEnd();                     webResponse.Close();                     responseStream.Close();                     return stringResponse;                 }                 catch (Exception)                 {                                    }             }                      }              }