C# HttpWebRequest 调用Web Service (Post)

HttpWebRequest myRequest = WebRequest.Create(url) as HttpWebRequest;
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
string requestParam = "abc=" + requestMsg;
byte[] param = System.Text.Encoding.UTF8.GetBytes(requestParam);
myRequest.ContentLength = param.Length;
Stream myRequestStream = myRequest.GetRequestStream();
myRequestStream.Write(param, 0, param.Length);
myRequestStream.Close();

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse() as HttpWebResponse; ;
Stream myResponseStream = myResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
responseMsg = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

 

posted on 2015-01-12 21:05  当时我就吓傻啦  阅读(197)  评论(0编辑  收藏  举报

导航