本文整理汇总了C#中RestTemplate.GetForObject方法的典型用法代码示例。如果您正苦于以下问题:C# RestTemplate.GetForObject方法的具体用法?C# RestTemplate.GetForObject怎么用?C# RestTemplate.GetForObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.GetForObject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
try
{
// Configure RestTemplate by adding the new converter to the default list
RestTemplate template = new RestTemplate();
template.MessageConverters.Add(new ImageHttpMessageConverter());
// Get image from url
#if NET_4_0
Bitmap nuGetLogo = template.GetForObjectAsync<Bitmap>("http://nuget.org/Content/Images/nugetlogo.png").Result;
#else
Bitmap nuGetLogo = template.GetForObject<Bitmap>("http://nuget.org/Content/Images/nugetlogo.png");
#endif
// Save image to disk
string filename = Path.Combine(Environment.CurrentDirectory, "NuGetLogo.png");
nuGetLogo.Save(filename);
Console.WriteLine(String.Format("Saved NuGet logo to '{0}'", filename));
}
#if NET_4_0
catch (AggregateException ae)
{
ae.Handle(ex =>
{
if (ex is HttpResponseException)
{
Console.WriteLine(((HttpResponseException)ex).GetResponseBodyAsString());
return true;
}
return false;
});
}
#else
catch (HttpResponseException ex)
{
Console.WriteLine(ex.GetResponseBodyAsString());
}
#endif
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.WriteLine("--- hit <return> to quit ---");
Console.ReadLine();
}
}
示例2: Search
public IList<IStreamableTrack> Search(string searchTerm)
{
RestTemplate template = new RestTemplate("http://api.soundcloud.com");
template.MessageConverters.Add(new DataContractJsonHttpMessageConverter());
IDictionary<string,object> vars = new Dictionary<string, object>();
vars.Add("client_id", playerID);
vars.Add("term", searchTerm);
var tracks = template.GetForObject<List<Track>>("/tracks.json?client_id={client_id}&order=hotness&q={term}", vars);
// Remove any tracks that aren't streamable
tracks.RemoveAll(track => !track.Streamable);
foreach (var track in tracks)
{
// Dodgy hack for now
track.StreamURL = track.StreamURL + "?client_id=" + playerID;
}
return tracks.ConvertAll(input => (IStreamableTrack) input);
}