0

我正在使用HttpRequestC# 中的 s 与网站交互
我可以读取页面内容,但是当我尝试将一些信息发布回服务器时,一切正常,但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;
 }
4

1 回答 1

0

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

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

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

更新:

request.ContentType = "multipart/form-data; charset: utf-8; boundary=" + boundary;
于 2012-11-30T22:13:39.117 回答