2010-08-25 238 views
1

我想使用的WebRequest和WebResponse對象通過C#來訪問網站,阿拉伯語的WebRequest 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); 
    } 
} 
+0

設置用戶代理字符串以包含阿拉伯語言。可能解決你的問題。 – 2010-08-25 12:19:51

回答

1

喜歡的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" 
+0

我添加了我使用的代碼,請讓我知道如何更新它 – 2010-08-26 07:32:57

+0

您可以通過以下字符串來控制支持的語言:「Accept-Language:ar」,其中ar代表阿拉伯語。有關語言的完整列表,請參閱: http://www.loc.gov/standards/iso639-2/php/code_list.php – 2010-08-26 13:51:16

0

這裏是你如何在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)