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();
            }
        }
  • 相关阅读:
    十四、内部类
    十三、模板设计模式
    十二、面向对象之多态
    三、泛型
    Spring Security学习笔记
    Maven构建web项目在Eclipse中部署的几种方法
    spring如何实现定时任务
    安装jar包到本地仓库
    使用java对pdf转成tiff文件
    word文档转pdf解决修订问题
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/14672834.html
Copyright © 2011-2022 走看看