• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

请高手赐教:在.net中通过HttpWebRequest向ASP页面中POST中文数据【"name=zhang帐张"】,asp中的request("name")出现乱码

guangjian 2003-04-09 11:47:12
源码:
asp部分:
<%@ codepage="936"%>
<%
response.ContentType = "text/xml"
Response.Write "<?xml version=" & AddQuotes("1.0") & " encoding=" & AddQuotes("gb2312") & "?>"
Response.Write "<a" & request("name") & "/>"
function AddQuotes(sText)
AddQuotes = """" & sText & """"
end function
%>


.net部分:
string strUrl="http://willpower/aa.asp";

string strData="name=zhang帐张";//aa.asp待接收的数据
// 与前置端会话
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

//req.ContentType = "application/unkown";
//req.ContentType ="text/xml;charset=gb2312";
StringBuilder UrlEncoded = new StringBuilder();
int i=0, j;
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;

#region 组织发送数据
while(i<strData.Length)
{
j=strData.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(strData.Substring(i, strData.Length-i)));
//UrlEncoded.Append(HttpUtility.UrlEncodeUnicode(strData.Substring(i, strData.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(strData.Substring(i, j-i)));
//string dd=strData.Substring(j,1);
UrlEncoded.Append(strData.Substring(j,1));
i = j+1;
}
#endregion

#region Post数据到服务器
//SomeBytes = Encoding.Unicode.GetBytes(UrlEncoded.ToString());
SomeBytes =System.Text.UTF8Encoding.ASCII.GetBytes(UrlEncoded.ToString());

//SomeBytes = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(strData));

req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Flush();
newStream.Close();
#endregion

#region 提取数据
HttpWebResponse result = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
MemoryStream memStream = FilterStream(ReceiveStream);
ReceiveStream.Close();
result.Close();

XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = false;
xmldoc.Load(memStream);
//ss中有乱码
string ss=xmldoc.InnerXml;
string ss1="";

#endregion
}
catch(Exception exp)
{

}
...全文
13 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ju_feng 2003-06-09
try req.ContentType ="text/xml;charset=uft-8";
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2003-04-09 11:47
社区公告

让您成为最强悍的C#开发者