使用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);
}
{
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);
}
相关文章推荐
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- WP7中HttpWebRequest的使用方法之POST方式
- dotnet 使用HttpWebRequest向Https Post数据时,需要证书确认的解决方法
- C#中HttpWebRequest与HttpWebResponse的使用方法
- Android中使用OkHttp包处理HTTP的get和post请求的方法
- 使用HttpWebRequest的POST取得网页内容(异步操作)
- C# 使用HttpWebRequest Post
- 使用HttpWebRequest发送自定义POST请求
- HttpRequest使用POST/GET方法
- https使用HttpWebRequest出现错误:System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.的解决方法
- 使用HttpWebRequest post数据时要注意UrlEncode
- 通过httpwebrequest实现http的post和get方法莫名出现500内部服务器错误的解决办法
- 使用HttpWebRequest的POST取得网页内容(异步操作)2篇集合
- C#通过WebClient/HttpWebRequest实现http的post/get方法与java对接
- kevinsawicki 的 HttpRequest使用Post方法请求数据
- 用C#使用HttpWebRequest Post数据时如何保持Session
- C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
- 不能以方法的方式使用不可调用的“system.web.httprequest.querystring”
- 在多线程环境下使用HttpWebRequest或者调用Web Service