使用HttpWebRequest 的 Post 方法处理简单的WEB service

本文介绍了一个使用SOAP协议发送短信的静态方法实现细节。该方法通过替换特殊字符来确保消息内容的正确传递,并构造了用于发送请求的XML字符串。此外,还展示了如何利用HttpWebRequest进行POST请求并与服务器交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static int StaticSendSMS(string Sender, string Receiver, string Message)
{
    Message
= Message.Replace("&", "&");
    Message
= Message.Replace("<", "&lt;");
    Message
= Message.Replace(">", "&gt;");
    Message
= Message.Replace("/"", "&quot;");
    Message
= Message.Replace("'", "&apos;");

    Encoding encode
= System.Text.Encoding.GetEncoding("utf-8");

   
string XmlStr = "<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"+
                       
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" xmlns:m0=/"http://etos.eastelsoft.com/v1/">/n" +
                       
"<SOAP-ENV:Body>/n" +
                       
"<m:sendSms xmlns:m=/"http://www.openuri.org//">/n" +
                       
"<m:request>/n" +
                       
"<m0:Sender>" + Sender + "</m0:Sender>/n" +
                       
"<m0:Receiver>" + Receiver + "</m0:Receiver>/n" +
                       
"<m0:Message>" + Message + "</m0:Message>/n" +
                       
"</m:request>/n" +
                       
"</m:sendSms>/n" +
                       
"</SOAP-ENV:Body>/n" +
                       
"</SOAP-ENV:Envelope>/n/n";

   
byte[] postByte = encode.GetBytes(XmlStr);

    HttpWebRequest myHttpWebRequest
= (HttpWebRequest)WebRequest.Create("http://220.250.64.142/etos2test/services/ETOS2SMS");

    myHttpWebRequest.Method
= "POST";
    myHttpWebRequest.ContentType
= "application/xop+xml; charset=utf-8; type=/"text/xml";
    myHttpWebRequest.ContentLength
= postByte.Length;

    Stream newStream
= myHttpWebRequest.GetRequestStream();
    newStream.Write(postByte,
0, postByte.Length);

    HttpWebResponse myHttpWebResponse
= (HttpWebResponse)myHttpWebRequest.GetResponse();
    Stream receiveStream
= myHttpWebResponse.GetResponseStream();

    StreamReader readStream
= new StreamReader(receiveStream, encode);

    Char[] read
= new Char[256];
    String ResponseText
= "";
   
int count = readStream.Read(read, 0, read.Length);
   
while (count > 0)
    {
        ResponseText
+= new String(read, 0, count);
        count
= readStream.Read(read, 0, read.Length);
    }

    readStream.Close();
    receiveStream.Close();
    myHttpWebResponse.Close();

   
string RS = "<Result xmlns=/"http://etos.eastelsoft.com/v1/">";
   
string RE = "</Result>";

   
int RSi = ResponseText.ToLower().IndexOf(RS.ToLower());
   
int REi = ResponseText.ToLower().IndexOf(RE.ToLower());

   
string Result = "-2";
   
if (RSi > 0)
        Result
= ResponseText.Substring(RSi + RS.Length, REi - RSi - RS.Length);

   
return int.Parse(Result);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值