>我正在使用 C#
中的 HttpRequest
s 与网站进行交互我可以读取页面内容,但是当我尝试将一些信息发布回服务器时,一切正常,但页面中TextArea
的值却无法正常工作......
我尝试为他们设置的文本是波斯语(波斯语),我收到的页面设置为 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;
}
更改表单元素以使用正确的编码:
<form method="post" accept-charset="Windows-1256" />
或<form accept-charset="utf-8" />
更新:
request.ContentType = "multipart/form-data; charset: utf-8; boundary=" + boundary;