UTF-8不适用于TextArea内容

时间:2012-11-30 21:58:14

标签: c# unicode httpwebrequest textarea jspx

我在C#中使用HttpRequest进行网站互动 我可以阅读页面内容但是当我尝试将一些信息发回服务器时,一切正常但不是页面中TextArea s的价值...
我试图为其设置的文字是波斯语(波斯语),我接收的页面设置为 Windows-1256 编码。 。

问题是textarea的内容没有像我在波斯语中所期望的那样显示!

更新:以下是我用来发送请求的代码:

public static string PostMultipart(string URL, List<RequestParameterInfo> parameters, List<string> cookies, string referer, string acceptLanguage, string acceptCharset, object KeepAliveValue)
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(URL));
   var boundary = "---------------------------7dc283270344";
   var messageBytes = Encoding.UTF8.GetBytes("--" + boundary);

   foreach (var item in parameters)
   {
      var prefixBytes = Encoding.UTF8.GetBytes("\n");
      var itemBytes = item.ToBytes();
      var postfixBytes = Encoding.UTF8.GetBytes("\n--" + boundary);

      var startIndex = messageBytes.Length;
      Array.Resize<byte>(ref messageBytes, messageBytes.Length + itemBytes.Length + prefixBytes.Length + postfixBytes.Length);

      prefixBytes.CopyTo(messageBytes, startIndex);
      itemBytes.CopyTo(messageBytes, startIndex + prefixBytes.Length);
      postfixBytes.CopyTo(messageBytes, startIndex + prefixBytes.Length + itemBytes.Length);
     }

     var lastPostfixBytes = Encoding.UTF8.GetBytes("--");
     var startIndex2 = messageBytes.Length;
     Array.Resize<byte>(ref messageBytes, messageBytes.Length + lastPostfixBytes.Length);
     lastPostfixBytes.CopyTo(messageBytes, startIndex2);




     request.Headers.Clear();
     request.Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*";
     request.Headers.Add(HttpRequestHeader.Pragma, "no-cache");
     request.ContentLength = messageBytes.Length;

     request.Referer = referer;
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
     request.ContentType = "multipart/form-data; boundary=" + boundary;
     request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
     request.Headers.Add(HttpRequestHeader.AcceptLanguage, acceptLanguage);

     if (!string.IsNullOrWhiteSpace(acceptCharset))
         request.Headers.Add(HttpRequestHeader.AcceptCharset, acceptCharset);

     if (KeepAliveValue is Boolean)
         request.KeepAlive = (bool)KeepAliveValue;
     else
         request.Headers.Add(HttpRequestHeader.KeepAlive, (string)KeepAliveValue);

     request.Expect = null;

     request.CookieContainer = new CookieContainer();
     foreach (var item in cookies)
         request.CookieContainer.SetCookies(new Uri(URL), item);

     request.AllowWriteStreamBuffering = true;
     request.Method = "POST";

     using (var strm = request.GetRequestStream())
     {
         strm.Write(messageBytes, 0, messageBytes.Length);
     }

     string resPage = "";
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
     {
         using (StreamReader srResponse = new StreamReader(response.GetResponseStream()))
         {
             resPage = srResponse.ReadToEnd();
         }
     }

     return resPage;
 }

1 个答案:

答案 0 :(得分:0)

更改您的Form元素以使用正确的编码:

<form method="post" accept-charset="Windows-1256" />

<form accept-charset="utf-8" />

<强>更新

request.ContentType = "multipart/form-data; charset: utf-8; boundary=" + boundary;