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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
K
Kaspersky official blog
A
Arctic Wolf
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 热门话题
N
News | PayPal Newsroom
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
博客园 - Franky
T
Tenable Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
J
Java Code Geeks
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
O
OpenAI News
The Cloudflare Blog
月光博客
月光博客
Google Online Security Blog
Google Online Security Blog
V
V2EX
罗磊的独立博客
美团技术团队
博客园 - 三生石上(FineUI控件)
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
大猫的无限游戏
大猫的无限游戏

博客园 - 网际飞狐

异步服务框架 如何在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