zoukankan      html  css  js  c++  java
  • 客户端如何调用WebService并且POST数据

    直接上代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;using System.Net;
    using System.IO;
    namespace ETCRM.WebService.Tester
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    //String Url = "http://localhost:3520/SynchDataInterface.asmx?op=SetSynchData";
    
                    //WeService Address,需要注意的是SetSynchData是WebService中公开的方法,
                    //不要用?op=SetSynchData,无法调用,提示500 内部错误
                    String Url = "http://localhost:3520/SynchDataInterface.asmx/SetSynchData";
    byte[] dataArray = Encoding.Default.GetBytes("需要Post到Server-side的data"); WebRequest req = WebRequest.Create(Url); req.Method = "POST"; req.ContentLength = dataArray.Length; req.ContentType = "application/x-www-form-urlencoded"; //创建输入流 Stream dataStream = req.GetRequestStream(); //发送请求 dataStream.Write(dataArray, 0, dataArray.Length); dataStream.Close(); //WebResponse res = req.GetResponse();// GetResponse blocks until the response arrives HttpWebResponse res = (HttpWebResponse)req.GetResponse(); Stream receiveStream = res.GetResponseStream(); //Read the stream into a string StreamReader sr = new StreamReader(receiveStream); string resultstring = sr.ReadToEnd(); Console.WriteLine(resultstring); Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Read(); } } } }

    只是一个Example,记录下来方便以后查阅,其他还需要注意的就是流的Close问题

    需要注意编码问题,最好统一改成UIT-8

     byte[] dataArray = Encoding.UTF8.GetBytes(XmlObjectHelper.ObjectToXML(postData));
  • 相关阅读:
    怎样在Windows下编译OpenVRML
    Virtools脚本语言(VSL)教程 结构
    Virtools脚本语言(VSL)教程 枚举
    基于Python及Wx的离线Blog发布工具Zoundry Raven
    Virtools脚本语言(VSL)教程 使用 GUID
    Khronos关于WebGL最新进展
    【转】在EditPlus中利用正则表达式替换字符串
    Virtools脚本语言(VSL)教程 全局变量bc与ac
    VrmlPad3.0发布
    WebGL样品与演示
  • 原文地址:https://www.cnblogs.com/wanglee/p/3152826.html
Copyright © 2011-2022 走看看