本文介绍了我如何检索Web服务中发送的值(方法发布或获取)(nameOfPage.asmx)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

桌面应用程序中的代码:



  private   void  button3_Click( object  sender,EventArgs e)
{
// Postvars
byte [] buffer = Encoding.ASCII .GetBytes( test = postvar& test2 = another);
// byte [] buffer = Encoding.ASCII.GetByteCount(label1.Text);
// 初始化,我们使用localhost
HttpWebRequest WebReq =(HttpWebRequest)WebRequest .Create( http://127.0.0.1/MyWebS.asmx);
// 我们的方法是post,否则缓冲区(postvars)将无用
WebReq.Method = POST;
// 我们对postvars使用form contentType。
WebReq.ContentType = application / x-www-form-urlencoded;
// 缓冲区的长度(postvars)用作contentlength。
WebReq.ContentLength = buffer.Length;
// 我们打开一个用于编写postvars的流
Stream PostData = WebReq .GetRequestStream();
// 现在我们编写
PostData.Write(buffer, 0 ,buffer.Length);
// 之后,我们关闭。结束总是很重要!
PostData.Close();

// 获取响应句柄
// HttpWebResponse WebResp =(HttpWebResponse)WebReq.GetResponse();

// 让我们显示一些有关回复的信息
// Console.WriteLine(WebResp.StatusCode);
// Console.WriteLine(WebResp.Server);

// 现在,我们读取响应(字符串)并输出它。
// Stream Answer = WebResp.GetResponseStream();
// StreamReader _Answer = new StreamReader(Answer);
// Console.WriteLine(_Answer.ReadToEnd());

}





谢谢。

解决方案

Code in desktop application :

private void button3_Click(object sender, EventArgs e)
        {
            // Postvars
            byte[] buffer = Encoding.ASCII.GetBytes("test=postvar&test2=another");
            //byte[] buffer = Encoding.ASCII.GetByteCount(label1.Text);
            //Initialization, we use localhost
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/MyWebS.asmx");
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            Stream PostData = WebReq.GetRequestStream();
            //Now we write 
            PostData.Write(buffer, 0, buffer.Length);
            // Afterwards, we close. Closing is always important!
            PostData.Close();
            
            //Get the response handle
            //HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            
            //Let's show some information about the response
            //Console.WriteLine(WebResp.StatusCode);
            //Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            //Stream Answer = WebResp.GetResponseStream();
            //StreamReader _Answer = new StreamReader(Answer);
            //Console.WriteLine(_Answer.ReadToEnd());
  
        }



Thanks.

解决方案


这篇关于我如何检索Web服务中发送的值(方法发布或获取)(nameOfPage.asmx)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:51