Httwebrequest調用webservice

來源:互聯網
上載者:User

標籤:wav   headers   except   continue   form   指令碼   ajax   windows   uri   

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 = 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.