繁体   English   中英

将javascript代码转换为c#-需要帮助

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM