国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

asp.net中WebResponse跨域訪問示例

2019-11-14 16:19:22
字體:
來源:轉載
供稿:網友

前兩天,一個朋友讓我幫他寫這樣一個程序:在asp.net里面訪問asp的頁面,把數據提交對方的數據庫后,根據返回的值(返回值為:OK或ERROR),如果為OK再把填入本地數據庫。當時,想當然,覺得很簡單,用js的xmlhttp ,如果根據response 的值是“OK”就執行提交本地數據庫。很快寫完發過去,讓朋友試試,一試發現不行,后來一問,原來是跨域訪問,我給忽略了,于是讓朋友把asp改成web service,可朋友說程序是合作公司做的,只會asp,不會用web service ,狂暈ing。沒辦法,只能請出asp.net的 WebResponse了,很多網站采集程序都是用這個。第一版寫完了,倒是可以跨域訪問了,不過是亂碼,調整有關編碼的方式,終于可以了。這個應用雖小可是涉及的知識點不少:

1、xmlhttp 不能跨域提交。

當然XMLHttPRequest還是權宜的解決的方法,

2、webresponse可以進行跨域訪問,不過要注意

1)、get和post的區別。
2)、注意Timeout的問題。

這些都是簡單的程序,記下來備忘,高手就不必看了。

不廢話了,下面是相關的c#代碼:

 

 代碼如下復制代碼
/// <summary>
        /// 使用Post方法發送數據
        /// </summary>
        /// <param name=”pi_strPostURl”>提交地址</param>
        /// <param name=”pi_strParm”>參數</param>
        /// <returns></returns>      
        public static string PostResponse(string pi_strPostURl, string pi_strParm)
        {
            try
            {
                //編碼
                Encoding t_Encoding = Encoding.GetEncoding(“GB2312“);
        Uri t_Uri = new Uri(pi_strPostURl);             
                byte[] paramBytes = t_Encoding.GetBytes(pi_strParm);
                WebRequest t_WebRequest = WebRequest.Create(t_Uri);
        t_WebRequest.Timeout = 100000;
                //設置ContentType
                t_WebRequest.ContentType = “application/x-www-form-urlencoded“;
               
                t_WebRequest.Method = EnumMethod.POST.ToString();                //初始化
                using (Stream t_REStream = t_WebRequest.GetRequestStream())
                {
                    //發送數據
                    requestStream.Write(paramBytes, 0
, paramBytes.Length);
                }
               
                WebResponse t_WebResponse =
 t_WebRequest.GetResponse();
               
                using (StreamReader t_StreamReader = new StreamReader(t_WebResponse .GetResponseStream(), t_Encoding))
                {
                    return t_StreamReader.ReadToEnd();
                }
            }
            catch
            {
                return “ERROR“;
            }
        }
 
public static string GetResponse(string pi_strPostURl, string pi_strParm)
        {
            try
            {
                //編碼
                Encoding t_Encoding = Encoding.GetEncoding(“GB2312“);              
                Uri t_Uri = new Uri(string.Format(“{0}?{1}“, pi_strPostURl, pi_strParm));
               
                WebRequest t_WebRequest =
 WebRequest.Create(t_Uri);
              
                t_WebRequest.Timeout = 100000;
                t_WebRequest.ContentType = “application/x-www-form-urlencoded“;
              
                t_WebRequest.Method = EnumMethod.GET.ToString(); 
                WebResponse t_WebResponse =
 t_WebRequest.GetResponse();
               
                using (StreamReader t_StreamReader = new StreamReader(t_WebResponse.GetResponseStream(), t_Encoding))
                {
                    return t_StreamReader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
public static string AtionResponse(string pi_Url, EnumMethod pi_Method)
        {
             string t_strUrlPath=“”;
             string t_parm = “”;           
             Uri  t_Url = new Uri(pi_Url);               
             t_parm= t_Url.Query;
             if (parmString.StartsWith(“?“))
                    t_parm = t_parm.Remove(0, 1);               
             t_strUrlPath = “http://“ + t_Url .Authority + t_Url .AbsolutePath;
            return GetResponse(t_strUrlPath, t_parm, pi_Method);
        }
 public enum EnumMethod
        {
            POST,
            GET
        }

現在jquery Ajax支持跨域了,下面看個實例我們可對上面進行處理成json數據即可

JQuery.getJSON也同樣支持jsonp的數據方式調用。

客戶端JQuery.ajax的調用代碼示例:

 代碼如下復制代碼

$.ajax({
 type : "get",
 async:false,
 url : "http://www.xxx.com/ajax.do",
 dataType : "jsonp",
 jsonp: "callbackparam",//服務端用于接收callback調用的function名的參數
 jsonpCallback:"success_jsonpCallback",//callback的function名稱
 success : function(json){
  alert(json);
  alert(json[0].name);
 },
 error:function(){
  alert('fail');
 }
});

服務端返回數據的示例代碼:

 代碼如下復制代碼
public void ProcessRequest (HttpContext context) {
 context.Response.ContentType = "text/plain";
 String callbackFunName = context.Request["callbackparam"];
 context.Response.Write(callbackFunName + "([ { name:"John"}])");
}

        而jquery.getScript方式處理的原理類似,也同樣需要服務端返回數據上做支持,不同的是服務端返回的結果不同。不是返回一個callback的function調用,而是直接將結果賦值給請求傳遞的變量名。客戶端則是像引入一個外部script一樣加載返回的數據 來源:http://www.111cn.net/net/net/56393.htm。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祥云县| 滕州市| 广河县| 永康市| 伊通| 房产| 德阳市| 孟州市| 岳阳市| 建昌县| 禹城市| 饶河县| 灵宝市| 万年县| 盐池县| 武清区| 荔浦县| 五原县| 沅陵县| 凤庆县| 武川县| 定西市| 长春市| 武邑县| 松江区| 永平县| 花莲市| 旌德县| 阿拉善右旗| 新郑市| 武平县| 依兰县| 石林| 龙江县| 讷河市| 永福县| 绥滨县| 溧阳市| 思茅市| 萨迦县| 普陀区|