本文整理汇总了C#中RestTemplate类的典型用法代码示例。如果您正苦于以下问题:C# RestTemplate类的具体用法?C# RestTemplate怎么用?C# RestTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RestTemplate类属于命名空间,在下文中一共展示了RestTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SearchButton_Click
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
// Note that you can also use the NJsonHttpMessageConverter based on Json.NET library
// that supports getting/setting values from JSON directly,
// without the need to deserialize/serialize to a .NET class.
IHttpMessageConverter jsonConverter = new DataContractJsonHttpMessageConverter();
jsonConverter.SupportedMediaTypes.Add(new MediaType("text", "javascript"));
RestTemplate template = new RestTemplate();
template.MessageConverters.Add(jsonConverter);
#if NET_3_5
template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
r =>
{
if (r.Error == null)
{
this.ResultsItemsControl.ItemsSource = r.Response.Data.Items;
}
}, this.SearchTextBox.Text);
#else
// Using Task Parallel Library (TPL)
var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}", this.SearchTextBox.Text)
.ContinueWith(task =>
{
if (!task.IsFaulted)
{
this.ResultsItemsControl.ItemsSource = task.Result.Data.Items;
}
}, uiScheduler); // execute on UI thread
#endif
}
示例2: RestBucksClient
public RestBucksClient()
{
// Create and configure RestTemplate
restTemplate = new RestTemplate(BaseUrl);
DataContractHttpMessageConverter xmlConverter = new DataContractHttpMessageConverter();
xmlConverter.SupportedMediaTypes = new MediaType[]{ new MediaType("application", "vnd.restbucks+xml") };
restTemplate.MessageConverters.Add(xmlConverter);
}
示例3: SetUp
public void SetUp()
{
template = new RestTemplate(uri);
template.MessageConverters = new List<IHttpMessageConverter>();
webServiceHost = new WebServiceHost(typeof(TestService), new Uri(uri));
webServiceHost.Open();
}
示例4: SetUp
public void SetUp()
{
template = new RestTemplate(uri);
contentType = new MediaType("text", "plain");
webServiceHost = new WebServiceHost(typeof(TestService), new Uri(uri));
webServiceHost.Open();
}
示例5: Registration
public Registration(RestRequestCreator requestCreator)
{
_restTemplate = requestCreator.GetRestTemplate();
// // 20141211
// // temporary
// // TODO: think about where to move it
// var tracingControl = new TracingControl("TmxClient_");
}
示例6: ConfigureRestTemplate
// Configure the REST client used to consume LinkedIn API resources
protected override void ConfigureRestTemplate(RestTemplate restTemplate)
{
restTemplate.BaseAddress = API_URI_BASE;
/*
// Register custom interceptor to set "x-li-format: json" header
restTemplate.RequestInterceptors.Add(new JsonFormatInterceptor());
*/
}
示例7: CreateServer
/// <summary>
/// Creates a <see cref="MockRestServiceServer"/> instance based on the given <see cref="RestTemplate"/>.
/// </summary>
/// <param name="restTemplate">The RestTemplate.</param>
/// <returns>The created server.</returns>
public static MockRestServiceServer CreateServer(RestTemplate restTemplate)
{
ArgumentUtils.AssertNotNull(restTemplate, "restTemplate");
MockClientHttpRequestFactory mockRequestFactory = new MockClientHttpRequestFactory();
restTemplate.RequestFactory = mockRequestFactory;
return new MockRestServiceServer(mockRequestFactory);
}
示例8: SetUp
public void SetUp()
{
template = new RestTemplate(uri);
template.MessageConverters = new List<IHttpMessageConverter>();
contentType = new MediaType("application", "json");
webServiceHost = new WebServiceHost(typeof(TestService), new Uri(uri));
webServiceHost.Open();
}
示例9: AbstractOAuth1ApiBinding
/// <summary>
/// Constructs the API template without user authorization.
/// This is useful for accessing operations on a provider's API that do not require user authorization.
/// </summary>
protected AbstractOAuth1ApiBinding()
{
this.isAuthorized = false;
this.restTemplate = new RestTemplate();
#if !SILVERLIGHT
((WebClientHttpRequestFactory)restTemplate.RequestFactory).Expect100Continue = false;
#endif
this.restTemplate.MessageConverters = this.GetMessageConverters();
this.ConfigureRestTemplate(this.restTemplate);
}
示例10: MovieViewModel
public MovieViewModel()
{
CreateMovieCommand = new DelegateCommand(CreateMovie, CanCreateMovie);
DeleteMovieCommand = new DelegateCommand(DeleteMovie, CanDeleteMovie);
template = new RestTemplate("http://localhost:12345/Services/MovieService.svc/");
template.MessageConverters.Add(new DataContractJsonHttpMessageConverter());
RefreshMovies();
}
示例11: AbstractOAuth2ApiBinding
/// <summary>
/// Constructs the API template with OAuth credentials necessary to perform operations on behalf of a user.
/// </summary>
/// <param name="accessToken">The access token.</param>
protected AbstractOAuth2ApiBinding(String accessToken)
{
this.accessToken = accessToken;
this.restTemplate = new RestTemplate();
#if !SILVERLIGHT
((WebClientHttpRequestFactory)restTemplate.RequestFactory).Expect100Continue = false;
#endif
this.restTemplate.RequestInterceptors.Add(new OAuth2RequestInterceptor(accessToken, this.GetOAuth2Version()));
this.restTemplate.MessageConverters = this.GetMessageConverters();
this.ConfigureRestTemplate(this.restTemplate);
}
示例12: SearchButton_Click
private void SearchButton_Click(object sender, EventArgs e)
{
this.ResultsFlowLayoutPanel.Controls.Clear();
IHttpMessageConverter njsonConverter = new NJsonHttpMessageConverter();
njsonConverter.SupportedMediaTypes.Add(new MediaType("text", "javascript"));
RestTemplate template = new RestTemplate();
template.MessageConverters.Add(njsonConverter);
template.GetForObjectAsync<JToken>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
delegate(RestOperationCompletedEventArgs<JToken> r)
{
if (r.Error == null)
{
foreach (JToken jToken in r.Response.Value<JToken>("responseData").Value<JArray>("results"))
{
PictureBox pBox = new PictureBox();
pBox.ImageLocation = jToken.Value<string>("tbUrl");
pBox.Height = jToken.Value<int>("tbHeight");
pBox.Width = jToken.Value<int>("tbWidth");
ToolTip tt = new ToolTip();
tt.SetToolTip(pBox, jToken.Value<string>("visibleUrl"));
this.ResultsFlowLayoutPanel.Controls.Add(pBox);
}
}
}, this.SearchTextBox.Text);
/*
template.GetForObjectAsync<GImagesResponse>("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q={query}",
delegate(RestOperationCompletedEventArgs<GImagesResponse> r)
{
if (r.Error == null)
{
foreach (GImage gImage in r.Response.Data.Items)
{
PictureBox pBox = new PictureBox();
pBox.ImageLocation = gImage.ThumbnailUrl;
pBox.Height = gImage.ThumbnailHeight;
pBox.Width = gImage.ThumbnailWidth;
ToolTip tt = new ToolTip();
tt.SetToolTip(pBox, gImage.SiteUrl);
this.ResultsFlowLayoutPanel.Controls.Add(pBox);
}
}
}, this.SearchTextBox.Text);
*/
}
示例13: CreateRestTemplate
protected override RestTemplate CreateRestTemplate()
{
RestTemplate template = new RestTemplate();
((WebClientHttpRequestFactory) template.RequestFactory).Expect100Continue = false;
IList<IHttpMessageConverter> list = new List<IHttpMessageConverter>(2);
FormHttpMessageConverter item = new FormHttpMessageConverter {
SupportedMediaTypes = { MediaType.ALL }
};
list.Add(item);
list.Add(new MsJsonHttpMessageConverter());
template.MessageConverters = list;
return template;
}
示例14: 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();
}
}
示例15: SetUp
public void SetUp()
{
mocks = new MockRepository();
requestFactory = mocks.StrictMock<IClientHttpRequestFactory>();
request = mocks.StrictMock<IClientHttpRequest>();
response = mocks.StrictMock<IClientHttpResponse>();
errorHandler = mocks.StrictMock<IResponseErrorHandler>();
converter = mocks.StrictMock<IHttpMessageConverter>();
IList<IHttpMessageConverter> messageConverters = new List<IHttpMessageConverter>(1);
messageConverters.Add(converter);
template = new RestTemplate();
template.RequestFactory = requestFactory;
template.MessageConverters = messageConverters;
template.ErrorHandler = errorHandler;
}