httpwebrequest调用webservice

客户端代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Test2.CallWebservice
{
    class Program
    {
        static void Main(string[] args)
        {
            MyServiceReference.MyTestSoapClient myTestSoapClient = new MyServiceReference.MyTestSoapClient();
            //Console.WriteLine(myTestSoapClient.getvalue(1,2));
            Console.WriteLine(HttpPost("http://localhost:8082/MyTest.asmx/getvalue"));
            Console.ReadLine();
        }

        private static string HttpPost(string url)
        {
            //组参数内容
            byte[] bSend = Encoding.UTF8.GetBytes("pName=123&json=789");

            //设置访问信息
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";//"text/plain";
            //封装数据
            request.ContentLength = bSend.Length;
            Stream requestStram = request.GetRequestStream();
            requestStram.Write(bSend, 0, bSend.Length);
            requestStram.Close();
            //获取返回数据

            WebResponse wr = request.GetResponse();
            Stream getStream = wr.GetResponseStream();
            XmlTextReader Reader = new XmlTextReader(getStream);
            Reader.MoveToContent();
            return Reader.ReadInnerXml();
            //byte[] currentChunk = new byte[2048];  // 缓存buffer
            //int rc = 0;  // 每次实际收到的字节数
            //using (MemoryStream ms = new MemoryStream())
            //{
            //    while ((rc = getStream.Read(currentChunk, 0, currentChunk.Length)) > 0)
            //    {
            //        ms.Write(currentChunk, 0, rc);  // 将当次收到的字节写入流
            //    }
            //    currentChunk = ms.ToArray();   // 将流转换为byte[]
            //}
            //request.Abort();
            //return System.Text.Encoding.Default.GetString(currentChunk);
        }
        private static string HttpGet(string url)
        {
            //组参数内容
            //byte[] bSend = Encoding.UTF8.GetBytes("pName=123&json=789");

            //设置访问信息
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";//"text/plain";
            //封装数据
            //request.ContentLength = bSend.Length;
            Stream requestStram = request.GetRequestStream();
            //requestStram.Write(bSend, 0, bSend.Length);
            requestStram.Close();
            //获取返回数据

            WebResponse wr = request.GetResponse();
            Stream getStream = wr.GetResponseStream();

            byte[] currentChunk = new byte[2048];  // 缓存buffer
            int rc = 0;  // 每次实际收到的字节数
            using (MemoryStream ms = new MemoryStream())
            {
                while ((rc = getStream.Read(currentChunk, 0, currentChunk.Length)) > 0)
                {
                    ms.Write(currentChunk, 0, rc);  // 将当次收到的字节写入流
                }
                currentChunk = ms.ToArray();   // 将流转换为byte[]
            }
            request.Abort();
            return System.Text.Encoding.Default.GetString(currentChunk);
        }


    }
}

 服务端代码:

 public class MyTest : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World ,Alex";
        }
        [WebMethod]
        public string getvalue(int pName,int json)
        {
            return (pName + json).ToString();
        }
    }

 

posted @ 2021-04-18 09:17  当年小清新  阅读(613)  评论(0编辑  收藏  举报