Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
asp.net steamAPI
@model WebApplication1.Models.SteamUser.SteamStats
<body>
<p>プレイヤーネーム:@Model.response.players[0].realname</p>
<p>アバター @Model.response.players[0].avatarfull</p>
</body>
@{
ViewBag.Title = "SteamAPI Form";
}
<h2>Steam ID入力</h2>
<form action="/SteamAPI/getName" method="post">
<div class="control-group">
<label class="control-label" for="steamid">SteamID</label>
<div class="controls">
<input class="text-box single-line" id="steamid" name="SteamID" type="text" value="" />
<span class="field-validation-valid help-inline" data-valmsg-for="SteamID" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-actions no-color">
<input type="submit" value="送信" class="btn" />
</div>
</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class SteamAPIController : Controller
{
// GET: SteamAPI
public ActionResult Index()
{
return View();
}
public String getName(string steamid)
{
try
{
// URLエンコーディング
string url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=STEAM-API-KEY&steamids=";
// HTTPアクセス
WebRequest req = WebRequest.Create(url + steamid);
WebResponse res = req.GetResponse();
// レスポンス(JSON)をオブジェクトに変換
SteamStats sstats;
using (res)
{
using (var resStream = res.GetResponseStream())
{
var serializer = new DataContractJsonSerializer(typeof(SteamStats));
sstats = (SteamStats)serializer.ReadObject(resStream);
}
}
// 結果を出力
return View(sstats);
}
catch (Exception)
{
throw;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace WebApplication1.Models
{
public class SteamUser
{
[DataContract]
public class SteamStats
{
[DataMember]
public Response response { get; set; }
}
[DataContract]
public class Response
{
[DataMember]
public List<Player> players { get; set; }
[DataContract]
public class Player
{
[DataMember]
public string steamid { get; set; }
[DataMember]
public string communityvisibilitystate { get; set; }
[DataMember]
public string profilestate { get; set; }
[DataMember]
public string personaname { get; set; }
[DataMember]
public long lastlogoff { get; set; }
[DataMember]
public string profileurl { get; set; }
[DataMember]
public string avatar { get; set; }
[DataMember]
public string avatarmedium { get; set; }
[DataMember]
public string avatarfull { get; set; }
[DataMember]
public string personastate { get; set; }
[DataMember]
public string realname { get; set; }
[DataMember]
public string primaryclanid { get; set; }
[DataMember]
public long timecreated { get; set; }
[DataMember]
public int personastateflags { get; set; }
[DataMember]
public string loccountrycode { get; set; }
[DataMember]
public string locstatecode { get; set; }
[DataMember]
public int loccityid { get; set; }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.