开发者

Arabic WebRequest C#

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);
    }
}


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"


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜