zoukankan      html  css  js  c++  java
  • 用Soap调用WebService

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Net;

    using System.Text;

    using System.Threading.Tasks;

    using System.IO;

    namespace

     Weather

    {

    classProgram

        {

           staticvoid Main(string[] args)

            {

              StringBuilder soap = newStringBuilder();

                soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

                soap.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\http://www.w3.org/2001/XMLSchema\ xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");

                soap.Append("<soap:Body>");

                soap.Append("<getWeatherbyCityName  xmlns=\"http://WebXml.com.cn/\">");

                soap.Append("<theCityName>北京</theCityName>");

                soap.Append("</getWeatherbyCityName >");

                soap.Append("</soap:Body>");

                soap.Append("</soap:Envelope>");

                string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";

                Console.WriteLine(GetSOAPReSource(url, soap.ToString()));

                Console.ReadKey();

            }

           publicstaticstring GetSOAPReSource(string url, string datastr)

            {

               //发起请求

         Uri uri = newUri(url);

         WebRequest webRequest = WebRequest.Create(uri);

                webRequest.ContentType ="text/xml; charset=utf-8";

                webRequest.Method ="POST";

                using (Stream requestStream = webRequest.GetRequestStream())

                {

                   byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());

                   requestStream.Write(paramBytes, 0, paramBytes.Length);

                }

               //响应

               WebResponse webResponse = webRequest.GetResponse();

               using (StreamReader myStreamReader = newStreamReader(webResponse.GetResponseStream(), Encoding.UTF8))

                {

                   string result = "";

                   return result = myStreamReader.ReadToEnd();

                }

            }

        }

    }

    生命不息,奋斗不止
  • 相关阅读:
    【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
    【spring源码学习】spring的IOC容器在初始化bean过程
    【spring源码学习】Spring的IOC容器之BeanPostProcessor接口学习
    Zookeeper之Zookeeper的Client的分析
    Zookeeper之Zookeeper底层客户端架构实现原理(转载)
    【LIUNX】目录或文件权限,权限授予
    Java 8 日期时间API使用介绍
    Java 8中的 Streams API 详解
    Java 8 Lambda表达式介绍
    Java中Comparable和Comparator区别小结
  • 原文地址:https://www.cnblogs.com/Griffin/p/2545324.html
Copyright © 2011-2022 走看看