handsome1234

博客园 首页 新随笔 联系 订阅 管理

ASP.net 中手工调用WS(POST方式)
核心代码:
string strUrl="http://localhost:21695/service1.asmx/getmythmod";
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(strUrl);
request.Method="POST";
request.ContentType="application/x-www-form-url"
string paraUrlCoded+="="+HttpUtility.UrlEncode(this.txtUserId.Text);
byte[] payload;
payload=System.Text.Encodeing.UTF8.GetBytes(paraUrlCoded);
request.ContentLength=payload.Length;

Stream writer=request.GetRequestStream();
writer.Write(payload,0,payload.Length);
writer.Close();

HttpWebResponse reponse=(HttpWebResponse)request.GetResponse();
Stream s=response.GetResponseStream();
XmlTextReader Reader=ReadInnerXml();
strValue=strValue.Replace("&lt;","<");
strValue=strValue.Replace("&gt;",">");
Regex rx=new Regex(@"(\r\n)+");
strValue=rx.replace(strValue,"");
this.ClientScript.RegisterStartupScript(typeof(string),null,"alert()"+strValue+")",ture);
Reader.Close();

 

注意事项:webservice所在的web.config里面要添加:
<webServices>
<protocols>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>

 

安全风险增加

posted on 2015-05-01 14:59  handsome1234  阅读(444)  评论(0编辑  收藏  举报