1

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);
    }
}
Hooked
  • 84,485
  • 43
  • 192
  • 261
Ahmad Hajou
  • 1,289
  • 5
  • 22
  • 39

2 Answers2

1

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"
Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42
  • I added the code I used, please let me know how can I update it – Ahmad Hajou Aug 26 '10 at 07:32
  • You can control languages that you support by the following string: "Accept-Language:ar", where ar for Arabic. For full list of languages see: http://www.loc.gov/standards/iso639-2/php/code_list.php – Boris Modylevsky Aug 26 '10 at 13:51
0

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)
Tareq Ibrahim
  • 365
  • 5
  • 18