阿拉伯语 WebRequest C#

发布于 2024-09-15 18:36:33 字数 1086 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-09-22 18:36:33

就像 Mikael 建议尝试这个:

HttpWebRequest request=(HttpWebRequest)WebRequest.Create("http://www.yourdomain.com");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1);Accept-Language:ar"

Like Mikael suggested try this one:

HttpWebRequest request=(HttpWebRequest)WebRequest.Create("http://www.yourdomain.com");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1);Accept-Language:ar"
め可乐爱微笑 2024-09-22 18:36:33

下面是在 vb.net 中执行此操作的方法:

Dim SW As StreamWriter
Dim ar As System.Text.UTF8Encoding = New System.Text.UTF8Encoding

Request.ContentLength = ar.GetByteCount(your_string)     ' Here
SW = New StreamWriter(Request.GetRequestStream(), ar)    ' And Here
SW.Write(your_string)

Here is how you do it in vb.net:

Dim SW As StreamWriter
Dim ar As System.Text.UTF8Encoding = New System.Text.UTF8Encoding

Request.ContentLength = ar.GetByteCount(your_string)     ' Here
SW = New StreamWriter(Request.GetRequestStream(), ar)    ' And Here
SW.Write(your_string)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文