惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
博客园 - 聂微东
爱范儿
爱范儿
罗磊的独立博客
P
Proofpoint News Feed
博客园 - Franky
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
美团技术团队
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
A
About on SuperTechFans
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog

博客园 - 网际飞狐

异步服务框架 如何在TFS中用命令行提交更新 CollabNet Subversion 输出自定义日期格式 - 网际飞狐 - 博客园 你正确关闭WCF链接了吗? 通过OperationContext添加消息头信息 PHP在II7安装指南 [Google App Engine] Hello, world! 在IIS7中配置使用Python 2008年12月小记(NewSequentialID(),ADO.NET Data Service,Visual Studio Tips,安装Django,JQuery智能感知) [OpenAPI] html标签分析 System.Web.Routing 使用基础 Notes for 2008-11(GetRange, backup,file hash, PostRequest, QueryString) 对硬编码WCF服务的封装(提供服务和客户端调用的封装,调用样例....) Observer Pattern, Delegate and Event How to view the W3WP process by c#? 项目框架概要 2008年10月小记(SQL删除重复记录,生成表结构,字符串特性,statistics io) WinDbg使用摘要
JS通过服务代理调用跨域服务
网际飞狐 · 2008-11-05 · via 博客园 - 网际飞狐

这里讲的是服务代理,应用情景如下:
 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