欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net中WebResponse 跨域訪問實(shí)例代碼

 更新時(shí)間:2014年01月20日 16:18:09   作者:  
一篇朋友很久前寫的asp.net中WebResponse 跨域訪問示例,下面我轉(zhuǎn)過來與大家一起學(xué)習(xí)學(xué)習(xí),希望文章對大家會有幫助

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

1、xmlhttp 不能跨域提交。

當(dāng)然XMLHttpRequest還是權(quán)宜的解決的方法,

2、webresponse可以進(jìn)行跨域訪問,不過要注意

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

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

不廢話了,下面是相關(guān)的c#代碼:

復(fù)制代碼 代碼如下:

/// <summary>
        /// 使用Post方法發(fā)送數(shù)據(jù)
        /// </summary>
        /// <param name=”pi_strPostURl”>提交地址</param>
        /// <param name=”pi_strParm”>參數(shù)</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;
                //設(shè)置ContentType
                t_WebRequest.ContentType = “application/x-www-form-urlencoded“;

                t_WebRequest.Method = EnumMethod.POST.ToString();                //初始化
                using (Stream t_REStream = t_WebRequest.GetRequestStream())
                {
                    //發(fā)送數(shù)據(jù)
                    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
        }

現(xiàn)在jquery ajax支持跨域了,下面看個(gè)實(shí)例我們可對上面進(jìn)行處理成json數(shù)據(jù)即可

JQuery.getJSON也同樣支持jsonp的數(shù)據(jù)方式調(diào)用。

客戶端JQuery.ajax的調(diào)用代碼示例:

復(fù)制代碼 代碼如下:

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

服務(wù)端返回?cái)?shù)據(jù)的示例代碼:

復(fù)制代碼 代碼如下:

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

而jquery.getScript方式處理的原理類似,也同樣需要服務(wù)端返回?cái)?shù)據(jù)上做支持,不同的是服務(wù)端返回的結(jié)果不同。不是返回一個(gè)callback的function調(diào)用,而是直接將結(jié)果賦值給請求傳遞的變量名??蛻舳藙t是像引入一個(gè)外部script一樣加載返回的數(shù)據(jù)。

相關(guān)文章

最新評論