JS通过服务代理调用跨域服务

这里讲的是服务代理,应用情景如下:
 A站点有一个JS脚本需要调用B站点的服务接口,但是A站点和B站点不是同一个域名下,这样就关系到跨域访问的了问题了,一种解决方法是通过一个代理去接管所有对B站点服务的调用。
服务器端:AcrossServer.ashx
public   class  AcrossServer : IHttpHandler {
    
    
public   void  ProcessRequest (HttpContext context) {
        context.Response.ContentType 
=   " text/plain " ;

        
string  clientKey  =  context.Request[ " ClientKey " ];
        
string  clientIP  =  context.Request[ " ClientIP " ];

        context.Response.Write(
string .Format( " AcrossServer:ClientKey={0}, ClientIP={1} " , clientKey, clientIP));
    }

}
代理服务:
public   class  AcrossClient : IHttpHandler {
    
    
public   void  ProcessRequest (HttpContext context) {
        context.Response.ContentType 
=   " text/plain " ;

        
// 构造Post数据
         string  clientKey  =   " af8b908b-7735-4db6-9d8f-eef05b2eef69 " ;
        
string  clientIP  =  context.Request.UserHostAddress;
        StringBuilder sb 
=   new  StringBuilder();
        sb.AppendFormat(
" ClientKey={0}&ClientIP={1} " , clientKey, clientIP);
        
foreach  ( string  key  in  context.Request.Form.AllKeys)
        {
            sb.AppendFormat(
" &{0}={1} " , key, context.Request.Form[key]);
        }
        
string  postData  =  sb.ToString();
        
byte [] data  =  Encoding.UTF8.GetBytes(postData);

        
// HttpWebRequest
         string  url  =   string .Format( " http://my2.csdn.net/Samples/CJB3/AcrossServer.ashx " );
        HttpWebRequest request 
=  (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method 
=   " POST " ;
        request.ContentType 
=   " application/x-www-form-urlencoded " ;
        request.ContentLength 
=  data.Length;
        request.Timeout 
=   10000 ; // 10s

        
string  content  =   string .Empty;
        
try
        {
            
// 把postdata写入HttpWebRequest
             using  (Stream requestStream  =  request.GetRequestStream())
            {
                requestStream.Write(data, 
0 , data.Length);
                requestStream.Close();
            }
            
// 获取HttpWebResponse
             using  (HttpWebResponse response  =  (HttpWebResponse)request.GetResponse())
            {
                
if  (response.StatusCode  ==  HttpStatusCode.OK)
                {
                    
using  (Stream stream  =  response.GetResponseStream())
                    {
                        
using  (StreamReader reader  =   new  StreamReader(stream, Encoding.UTF8))
                        {
                            content 
=  reader.ReadToEnd();
                            reader.Close();
                        }
                        stream.Close();
                    }
                }
                response.Close();
            }
        }
        
catch  (System.Net.WebException)
        {
            
// TODO:考虑在此添加超时异常的日志
        }
        
finally
        {
            
if  (request  !=   null )
            {
                request.Abort();
                request 
=   null ;
            }
        }
        
        context.Response.Write(
string .Format( " AcrossClient=>{0} " , content));
    }

}
我们通过在代理端重新封装所有的Post数据并且添加新的服务验证等数据一起Post到真正的服务接口,最后把调用的返回也返回给代理调用方。

当然,我们可以对Post操作进一步封装到一个方法
PostRequest

你可能感兴趣的:(js)