阿拉伯语 WebRequest C#
我正在尝试使用 WebRequest 和 WebResponse 对象通过 C# 访问网站,
我登录到该网站并保留 cookie 以进一步浏览它, 问题是该网站是阿拉伯语,不知何故我从该网站收到一条格式化消息,表明我的浏览器不支持阿拉伯语。
也许我可以向请求对象添加一些内容,以确保该网站支持阿拉伯语。
这是我使用的代码,请让我知道如何更新它:
string formUrl = "http://www.kuwaitlook.com/Ar/Residential.asp";
string formParams = string.Format("Mega={0}", searchTarget);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1);Accept-Language:ar";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.Headers.Add("Cookie", cookieHeader);
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream()) {
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
StreamReader streamReader = new StreamReader(resp.GetResponseStream());
using (StreamWriter writer = new StreamWriter("text.xml")) {
string line;
while ((line = streamReader.ReadLine()) != null) {
writer.WriteLine(line);
}
}
I am trying to access a website through C# using the WebRequest and the WebResponse object,
I logged on to the site and preserved the cookie to further browse it,
The problem is that the website is arabic and somehow I got a formatted message from the website indicating that my browser does not support arabic.
Perhaps I can add something to the request object to ensure the website that arabic is supported.
This is the code I used, please let me know how to update it:
string formUrl = "http://www.kuwaitlook.com/Ar/Residential.asp";
string formParams = string.Format("Mega={0}", searchTarget);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1);Accept-Language:ar";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.Headers.Add("Cookie", cookieHeader);
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream()) {
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
StreamReader streamReader = new StreamReader(resp.GetResponseStream());
using (StreamWriter writer = new StreamWriter("text.xml")) {
string line;
while ((line = streamReader.ReadLine()) != null) {
writer.WriteLine(line);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像 Mikael 建议尝试这个:
Like Mikael suggested try this one:
下面是在 vb.net 中执行此操作的方法:
Here is how you do it in vb.net: