• 提交(post)xml文件给指定url的2种方法


    1  这段代码是在网上搜到的,拿来共享,项目正好要用到。其中的data你只需要传递一个xml字符串就可以

    protected   string   PostXmlToUrl(string   url,string   data)  
      {  
      HttpWebRequest   hwr   =   (HttpWebRequest)HttpWebRequest.Create(url);  
      hwr.Method   =   "POST";  
       
      Stream   stream   =   hwr.GetRequestStream();  
       
      StreamWriter   sw   =   new   StreamWriter(stream,System.Text.Encoding.UTF8);  
      sw.Write(data);  
      sw.Close();  
       
      stream   =   hwr.GetResponse().GetResponseStream();  
       
      StreamReader   sr   =   new   StreamReader(stream,System.Text.Encoding.UTF8);  
      string   ret   =   sr.ReadToEnd();  
      sr.Close();  
       
      return   ret;  
       
      }  

    2  利用xmlhttp对象,来发送请求.
       StreamReader sr=new StreamReader(Server.MapPath(XMLPathFull));
       MSXML2.XMLHTTPClass xmlHttp = new MSXML2.XMLHTTPClass();  
       xmlHttp.open("post",strURL,false,"","");
     
       xmlHttp.setRequestHeader  ("Content-type","text/xml;charset=UTF-8");
       xmlHttp.send(sr.ReadToEnd());
       //得到response
       string backxmlstring=xmlHttp.responseText.ToString();


    解读这个响应的话只需要2行代码,
    byte[]   buf   =   Request.BinaryRead(Request.ContentLength);  
      string   str   =   System.Text.Encoding.UTF8.GetString(buf);  
    这样就能得到。
    其实以上代码就可以建立一个简单的服务器间的交互模式,模拟的b/s架构,跟使用webservice与服务器间交互差不多,soap和http其实比较类似的。

  • 相关阅读:
    FirewallD 详解
    EventBus源码解析
    详谈 Jquery Ajax 异步处理Json数据.
    jQuery Ajax异步处理Json数据详解
    7款jQuery图片轮播滑动插件
    分享15款为jQuery Mobile定制的插件
    在springmvc中解决FastJson循环引用的问题
    SpringMVC与fastjson整合并同时解决中文乱码问题
    SpringMVC处理ajax请求
    spring mvc 返回json的配置
  • 原文地址:https://www.cnblogs.com/MaxIE/p/1851001.html
Copyright © 2020-2023  润新知