HttpWebRequest post提交XMl参数请求,

public  string  StartPing( string  pingURL) //改为一个传参数URL
{
StreamReader sr = null ;
StringBuilder sb = new  StringBuilder();
string  strOutput = string .Empty;
string  strXML = string .Empty;
try
{
//HttpUtility.HtmlEncode
strXML = BuildXML();
int  len = strXML.Length;
ASCIIEncoding ascii = new  ASCIIEncoding();
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(pingURL);
webReq.Method = "POST" ;
webReq.Timeout = 3000; //超时为3秒
webReq.ContentType = "text/xml" ;
//string contenttype = "application/x-www-form-urlencoded";//更网站该方法支持的类型要一致
webReq.Credentials = CredentialCache.DefaultNetworkCredentials;
webReq.ContentLength = len;
 
Stream streamRequest = webReq.GetRequestStream();
byte [] bt = ascii.GetBytes(strXML);
streamRequest.Write(bt, 0, len);
HttpWebResponse webRes = (HttpWebResponse)webReq.GetResponse();
 
sr = new  StreamReader(webRes.GetResponseStream(), Encoding.ASCII);
string  ret = sr.ReadToEnd();
sb.AppendLine( "\n" );
sb.AppendLine(ret);
 
if  (ret.IndexOf( "<boolean>0</boolean>" ) >= 0)
{
sb.AppendLine( "\n\t 服务提交成功!" );
}
else  if  (ret.IndexOf( "<boolean>1</boolean>" ) >= 0)
{
sb.AppendLine( "\n\t 服务提交失败!" );
}
if  (sr != null )
{
sr.Close();
}
}
catch  (Exception ex)
{
sb.AppendLine(ex.Message);
sb.AppendLine(ex.StackTrace);
}
finally
{
strOutput = sb.ToString();
if  (sr != null )
{
sr.Dispose();
}
}
return  strOutput;
}
 
private  string  BuildXML()
{
System.Text.StringBuilder sb = new  System.Text.StringBuilder();
sb.Append( "<?xml version=\"1.0\"?>" );
sb.Append( "<params>" );
sb.AppendFormat( "<title>{0}" , "公告标题" );
sb.Append( "</title>" );
sb.AppendFormat( "<content>{0}" , "公告内容,base64加密的" );
sb.Append( "</content>" );
 
sb.Append( "<verify=be10c510b5f4f082dbd4268820cde895>" );
 
sb.Append( "</params>" );
return  sb.ToString();
}

  

下面是服务器端接收请求方法:

加载事件里:
//对应方法StartPing
             byte [] byts = new  byte [Request.InputStream.Length];
             Request.InputStream.Read(byts, 0, byts.Length);
             string  req = System.Text.Encoding.Default.GetString(byts);
             req = Server.UrlDecode(req);
             //下面是接收到的内容
             //<?xml version="1.0"?><params><title>????</title><content>?????base64???</content><verify=be10c510b5f4f082dbd4268820cde895></params>
             */

  

分类:  ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2013/03/18/2966268.html ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值