【问题标题】:Translate javascript code to c# - help is needed将 javascript 代码翻译成 c# - 需要帮助
【发布时间】:2014-02-18 08:16:07
【问题描述】:

我有一个使用 .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

【问题讨论】:

    标签: c# javascript html http httpwebrequest


    【解决方案1】:

    这是一段 PHP 脚本,它使用 cURL 库向具有一系列特定标头和值的服务器发出请求。

    您将希望使用 .NET 等价物,WebRequest:http://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx 并查看cURL with user authentication in C# 以获取更多实施示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2013-07-07
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多