There can be no Triumph without Loss,No Victory without Suffering,No Freedom without Sacrifice.
All you have to decide is what to do with the time that is given to you.
Get busy Living, or Get busy Dying?
  首页 | 留言给我 | 订阅 Rss | CLI | 黄白之恋 Posts:158   Hits: 5068730    Comments: 173    
 日历归档
<<  <  2024 - 04  >  >>
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
282930
 About Me
 Name: ZhangSichu
 Sex: Male
 Age: 32
 Email: ZhangSichu@gmail.com
 MSN: ZhangSichu@hotmail.com
 Home: ZhangSichu.com
 WeiBo: weibo.com/zhangsichu
 个人推荐
 分类归档
  ·C++/C(5)  RSS
  ·软件工程(1)  RSS
  ·杂事/随感(26)  RSS
  ·.Net/Java(30)  RSS
  ·面向对象程序设计(5)  RSS
  ·汇编/破解(0)  RSS
  ·平面设计(3)  RSS
  ·SQL(5)  RSS
  ·COM/COM+(2)  RSS
  ·Web开发(81)  RSS
 My Friends
Back Forward Refresh Home 2024年4月23日 星期二 RSS CLI Mine Sweeper. In Javascript.

  Asp.Net程序向Asp程序提交数据发生的问题
字体大小 [ ]

问题描述:
实际环境中有两套程序。一套是Asp的新闻信息系统,向外界发布信息。一套是Asp.Net写的信息采集系统,采集外界信息。新闻采集系统根据编辑的要求,采集指定站点的信息并存储在本地服务器上的数据库。信息采集程序也运行在这个服务器上。当采集程序采集完成。编辑登录本地服务器上的一个站点挑选合适的新闻,点击发送,这条新闻就发送到了公开站点上的Asp的新闻信息系统。在发送信息的这个过程中Asp.Net程序要向Asp程序提交数据。

问题分析:
Asp.Net程序向Asp程序提交数据这个操作是跨站点数据提交。开始想使用SOAP WebService。用Asp写一个WSDL UDDI 想想就觉得比较恐怖。后来想想还是仿照REST 协议通过URL来提交数据吧。

问题实现:
Asp.Net 通过HttpRequest 访问Asp程序的一个Url (例如:savefetchinfo.asp?pN1=pV1&pN2=pV2)来传数据。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(savefetchinfo.asp?pN1=row["Title"].ToString()&pN2=row["Content"].ToString());
request.ContentType = "application/x-www-form-urlencoded";
request.GetResponse();

这段代码发送的数据量一大Asp程序就会500错。提示数据过大。当数据小的时候。中文也会出现乱码。这种方法发送的方式不对,数据全部从Url走的,编码也没有设置好。

查了google和MSDN换成了WebClient()
WebClient client = new WebClient();
NameValueCollection values = new NameValueCollection();
values.Add("pN1",row["Title"].ToString());
values.Add("pN2", row["Content"].ToString());
client.UploadValues("http://xxx.xxx.xxx/savefetchinfo.asp", values);

改成这样后数据能够全部提交,Asp程序不会500错。但是提交的中文还是乱码。并且在第二次第三次提交的时候会出现一些十分诡异的错误:"A connection that was expected to be kept alive was closed by the server" 或者是 "An established connection was aborted by the software in your host machine" 这样错误让人很是不解。后来反射了WebClient,发现WebClient内部是使用 WebRequest 来发送请求的。发送数据使用了
Stream requestStream = request.GetRequestStream();

取得发送数据的流。用stream来添加发送的内容。

经过上面的几次实验终于找到了一个好的办法:

string data = string.Concat(“pN1=", HttpUtility.UrlEncode(row["Title"].ToString(), System.Text.Encoding.GetEncoding("gb2312")),
"&pN2=", HttpUtility.UrlEncode(row["Content"].ToString(), System.Text.Encoding.GetEncoding("gb2312")));
byte[] buffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(data); /*fix encoding bug*/
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxx.xxx.xxx/savefetchinfo.asp");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
request.KeepAlive = false; /*fix connection closed bug*/
request.ProtocolVersion = HttpVersion.Version10;
Stream stream = request.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
stream.Close();
request.GetResponse();
  Posted @ 9/15/2007 1:07:23 PM | Hits (65850) | Comment (0

  Post Comment
标题 *
作者 *
密码 记住我
评论 *
    


Stable in Firefox 1.5 2.0Stable in IE6 IE7Stable in MozillaStable in Netscape
ZhangSichu.com V0.1.7507
Powered By ZhangSichu
Copyright © ZhangSichu
Download ZhangSichu.com source code. Download source code