zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    Docker容器案例:应用 Mysql
    rpm 命令参数使用详解
    MySQL中的两种临时表
    Yum本地Rpm库设置
    编程学习 博客
    yum -------包安装库
    Linux 基础 —— RPM
    在CentOS上编译安装PostgreSQL
    Linux上安装JDK环境变量配置
    yum_rpm(利用dvd建立本地yum库)
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/14672834.html
Copyright © 2011-2022 走看看