首页 > 代码库 > Httwebrequest调用webservice
Httwebrequest调用webservice
webservice的webconfig配置,不配置后面会报远程服务器返回错误: (500) 内部服务器错误。
<system.web> <compilation debug="true" targetFramework="4.0" /> <webServices> <protocols> <add name= "HttpPost"/> <add name= "HttpGet"/> </protocols> </webServices> </system.web>
webserviceDemo.asmx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace News.webservice { /// <summary> /// WebServiceDemo 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class WebServiceDemo : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string name,string name1) { return name+name1; } } }
后台调用 转自:http://www.cnblogs.com/ghelement/p/5286630.html
static void Main(string[] args) { string a= GetStringByUrl("http://localhost:1071/webservice/WebServiceDemo.asmx/HelloWorld?name=youchim"); string b= RequestWebService("http://localhost:1071/webservice/WebServiceDemo.asmx/HelloWorld","name=朱一届&name1=youchim"); } //get public static string GetStringByUrl(string strUrl) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl); req.UserAgent = "MSIE6.0"; req.Method = "GET"; //http://www.cnblogs.com/cresuccess/archive/2009/12/09/1619977.html HttpWebResponse res; try { res = (HttpWebResponse)req.GetResponse(); } catch (WebException ex) { res = (HttpWebResponse)ex.Response; } StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("UTF-8")); string strHtml = sr.ReadToEnd(); sr.Close(); res.Close(); return strHtml; } //post方法 public static string RequestWebService(string strUrl, string strPostData) { try { //构造请求 HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(strUrl); hwrRequest.Method = "POST"; hwrRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"; hwrRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); hwrRequest.Headers.Add("Accept-Language", "zh-cn"); hwrRequest.Headers.Add("Cache-Control", "gzip, deflate"); hwrRequest.Headers.Add("KeepAlive", "TRUE"); hwrRequest.Headers.Add("ContentLength", strPostData.Length.ToString()); hwrRequest.ContentType = "application/x-www-form-urlencoded"; hwrRequest.Referer = strUrl; hwrRequest.Headers.Add("UA-CPU", "x86"); hwrRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; hwrRequest.Timeout = 30000; hwrRequest.ServicePoint.Expect100Continue = false; //发送请求 byte[] bytPostData =http://www.mamicode.com/ Encoding.UTF8.GetBytes(strPostData); Stream strStream = hwrRequest.GetRequestStream(); strStream.Write(bytPostData, 0, bytPostData.Length); strStream.Close(); //就收应答 HttpWebResponse hwrResponse = (HttpWebResponse)hwrRequest.GetResponse(); Stream strStream1 = null; if (hwrResponse.ContentEncoding == "gzip") { System.IO.Compression.GZipStream gzsStream = new System.IO.Compression.GZipStream(hwrResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); strStream1 = gzsStream; } else { strStream1 = hwrResponse.GetResponseStream(); } string strResult = new StreamReader(strStream1, System.Text.Encoding.UTF8).ReadToEnd(); hwrResponse.Close(); return strResult; } catch (Exception excResult) { return ""; } }
Httwebrequest调用webservice
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。