• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
真老K
博客园    首页    新随笔    联系   管理    订阅  订阅

HttpWebRequest的个人见解

 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)
                {
                  
                }
            }
            
        }

      

    }

  

posted @ 2020-09-17 11:42  真老K  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3