[英]Translate javascript code to c# - help is needed
我有一個使用.net 4的Windows應用程序。
我正在從“ WarGaming.Net” API獲取數據。
我通過HttpWebRequest和HttpWebResponse類獲取的大多數數據。
我的網址請求是“ http://cw.worldoftanks.eu/clanwars/maps/provinces/regions/1/?ct=json ”
但是我得到“ 403 Forbidden”。
我閱讀了有關如何使用Java腳本https://github.com/thunder-spb/wot-api-description/issues/28#issuecomment-33958289的文章
function getResp($parr) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://cw.worldoftanks.eu".$parr);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Accept: application/json, text/javascript, text/html, */*',
'X-Requested-With: XMLHttpRequest'
)
);
curl_setopt($ch, CURLOPT_REFERER, "http://worldoftanks.eu/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$c_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if (curl_getinfo($ch, CURLINFO_CONTENT_TYPE) == 'image/jpeg') {
$response = array();
$response['request_data']['error_message'] = w2u('Site returned JPEG, Maintanace maybe?');
$response = json_encode($response);
}
curl_close($ch);
return $response;
}
print_r( getResp('/clanwars/maps/provinces/regions/1/?ct=json') );
而且我想知道如何將此請求隱含到具有.net元素的C#代碼中。
謝謝
RC
這是PHP腳本的一大塊,它使用cURL庫向具有一系列特定標頭和值的服務器發出請求。
您將要使用.NET等效WebRequest: http : //msdn.microsoft.com/zh-cn/library/system.net.webrequest%28v=vs.110%29.aspx並查看cURL使用C#中的用戶身份驗證獲取更多實現示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.