您的位置:首页 > 理论基础 > 计算机网络

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

2008-11-10 14:20 561 查看
public static int StaticSendSMS(string Sender, string Receiver, string Message)
{
Message = Message.Replace("&", "&");
Message = Message.Replace("<", "<");
Message = Message.Replace(">", ">");
Message = Message.Replace("/"", """);
Message = Message.Replace("'", "'");

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: