c#的httpWebRequest(2)

2021-07-16 12:15

阅读:642

public string httpWebservice(string url, string body)
        {
            string rootUrl = ConfigHelper.AppSettings("RemoteHttpURL");
            WebRequest wRequest = WebRequest.Create(rootUrl + url);
            wRequest.Method = "POST";
            wRequest.Timeout = 20000;
            wRequest.ContentType = "application/x-www-form-urlencoded";

            byte[] btBodys = Encoding.UTF8.GetBytes(body);
            wRequest.ContentLength = btBodys.Length;
            wRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

            WebResponse wResponse = wRequest.GetResponse();
            Stream stream = wResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
            string str = reader.ReadToEnd();   //url返回的值  
            reader.Close();
            wResponse.Close();
            return str;
        }


评论


亲,登录后才可以留言!