---
title: "WebResponse 跨域访问"
author: "cjc"
date: "2008-07-07T03:31:00Z"
tags: []
slug: "webresponse"
draft: false
lastmod: "2008-07-07T03:31:00Z"
id: 402711
url: "https://cjc.iteye.com/blog/402711"
---

&lt;script&gt;function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}&lt;/script&gt;

前两天，一个朋友让我帮他写这样一个程序：在asp.net里面访问asp的页面，把数据提交对方的数据库后，根据返回的值（返回值为：OK或ERROR），如果为OK再把填入本地数据库。当时，想当然，觉得很简单，用js的xmlhttp ，如果根据response 的值是“OK”就执行提交本地数据库。很快写完发过去，让朋友试试，一试发现不行，后来一问，原来是跨域访问，我给忽略了，于是让朋友把asp改成web service，可朋友说程序是合作公司做的，只会asp，不会用web service ，狂晕ing。没办法，只能请出asp.net的 [WebResponse](http://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx)了，很多网站采集程序都是用这个。第一版写完了，倒是可以跨域访问了，不过是乱码，调整有关编码的方式，终于可以了。这个应用虽小可是涉及的知识点不少： 1、xmlhttp 不能跨域提交。 当然XMLHttpRequest还是权宜的解决的方法，可以参考[思归](http://blog.joycode.com/saucer/Default.aspx)写的一篇文章：[http://blog.joycode.com/saucer/archive/2006/10/03/84572.aspx](http://blog.joycode.com/saucer/archive/2006/10/03/84572.aspx)

2、[webresponse可以进行跨域访问](http://www.watch-life.net/aspnet/webresponse-cross-site-visit.html)，不过要注意 1）、get和post的区别。 2）、注意Timeout的问题。

这些都是简单的程序，记下来备忘，高手就不必看了。

不废话了，下面是相关的c#代码：

{

{

Uri t\_Uri

WebRequest t\_WebRequest

t\_WebRequest.Timeout

t\_WebRequest.Method

{

, paramBytes.Length);

}

WebResponse t\_WebResponse

t\_WebRequest.GetResponse();

{

}

}

{

}

}

{

{

Uri t\_Uri

WebRequest t\_WebRequest

WebRequest.Create(t\_Uri);

t\_WebRequest.Timeout

t\_WebRequest.ContentType

t\_WebRequest.Method

WebResponse t\_WebResponse

t\_WebRequest.GetResponse();

{

}

}

{

}

}

{

Uri t\_Url

t\_parm

t\_parm

t\_strUrlPath

}

{

POST,

GET

}

///&lt;summary&gt;&lt;/summary&gt;/// 使用Post方法发送数据////// &lt;param name="”pi\_strPostURl”"&gt;提交地址/// &lt;param name="”pi\_strParm”"&gt;参数///&lt;returns&gt;&lt;/returns&gt; publicstaticstring PostResponse(string pi\_strPostURl, string pi\_strParm)try//编码 Encoding t\_Encoding = Encoding.GetEncoding(“GB2312“); =new Uri(pi\_strPostURl); byte\[] paramBytes = t\_Encoding.GetBytes(pi\_strParm);= WebRequest.Create(t\_Uri);=100000;//设置ContentType t\_WebRequest.ContentType = “application/x-www-form-urlencoded“;= EnumMethod.POST.ToString(); //初始化 using (Stream t\_REStream = t\_WebRequest.GetRequestStream())//发送数据 requestStream.Write(paramBytes, 0=using (StreamReader t\_StreamReader =new StreamReader(t\_WebResponse .GetResponseStream(), t\_Encoding))return t\_StreamReader.ReadToEnd();catchreturn “ERROR“;publicstaticstring GetResponse(string pi\_strPostURl, string pi\_strParm)try//编码 Encoding t\_Encoding = Encoding.GetEncoding(“GB2312“); =new Uri(string.Format(“{0}?{1}“, pi\_strPostURl, pi\_strParm));==100000;= “application/x-www-form-urlencoded“;= EnumMethod.GET.ToString(); =using (StreamReader t\_StreamReader =new StreamReader(t\_WebResponse.GetResponseStream(), t\_Encoding))return t\_StreamReader.ReadToEnd();catch (Exception e)return e.ToString();publicstaticstring AtionResponse(string pi\_Url, EnumMethod pi\_Method)string t\_strUrlPath=“”;string t\_parm = “”; =new Uri(pi\_Url); = t\_Url.Query;if (parmString.StartsWith(“?“))= t\_parm.Remove(0, 1); = “http://“ + t\_Url .Authority + t\_Url .AbsolutePath; return GetResponse(t\_strUrlPath, t\_parm, pi\_Method);publicenum EnumMethod

本文首发地址：[http://www.watch-life.net/aspnet/webresponse-cross-site-visit.html](http://www.watch-life.net/aspnet/webresponse-cross-site-visit.html)

更多文章见：[守望轩](http://www.watch-life.net/)\[[http://www.watch-life.net](http://www.watch-life.net/)]