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("<","<");
strValue=strValue.Replace(">",">");
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>
安全风险增加