本文整理汇总了C#中System.Net.Http.HttpClient.FollowLinkAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.FollowLinkAsync方法的具体用法?C# HttpClient.FollowLinkAsync怎么用?C# HttpClient.FollowLinkAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.FollowLinkAsync方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Returns_content_if_response_is_OK
public async Task Returns_content_if_response_is_OK()
{
string content = Guid.NewGuid().ToString();
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(content);
var httpClient = new HttpClient(new FakeHandler
{
Response = response,
InnerHandler = new HttpClientHandler()
});
var link = new GuidServiceLink();
Guid guid = Guid.Empty;
var machine = new HttpResponseMachine();
machine.AddResponseHandler(async (lr, r) =>
{
{
guid = Guid.Parse( r.Content.ReadAsStringAsync().Result);
return r;
}
}, HttpStatusCode.OK);
await httpClient.FollowLinkAsync(link,machine);
Assert.Equal(content, guid.ToString());
}
示例2: SpecifyHandlerChainForAboutLink
public Task SpecifyHandlerChainForAboutLink()
{
var foo = false;
var bar = false;
var baz = false;
var registry = new LinkFactory();
var grh = new InlineResponseHandler((rel,hrm) => baz = true,
new InlineResponseHandler((rel, hrm) => foo = true,
new InlineResponseHandler((rel, hrm) => bar = true)));
var machine = new HttpResponseMachine();
machine.AddResponseHandler(grh.HandleResponseAsync, System.Net.HttpStatusCode.OK);
var link = registry.CreateLink<AboutLink>();
link.Target = new Uri("http://example.org");
var httpClient = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()});
return httpClient.FollowLinkAsync(link,machine).ContinueWith(t =>
{
Assert.True(foo);
Assert.True(bar);
Assert.True(baz);
});
}
示例3: TestLink
public void TestLink()
{
var link = new GuidServiceLink()
{
Target = new Uri("http://localhost/guidservice")
};
var httpClient = new HttpClient();
httpClient.FollowLinkAsync(link);
}
示例4: Test
public Task Test()
{
var link = new Link(){Target = new Uri("http://localhost")};
link.HttpResponseHandler = new NotFoundHandler(new OkHandler(null));
var client = new HttpClient(new FakeHandler() {Response = new HttpResponseMessage() {StatusCode = HttpStatusCode.NotFound}});
return client.FollowLinkAsync(link);
}
示例5: Test
public async Task Test()
{
var link = new Link(){Target = new Uri("http://localhost")};
var notFoundHandler = new NotFoundHandler(new OkHandler(null));
var machine = new HttpResponseMachine();
machine.AddResponseHandler(notFoundHandler.HandleResponseAsync, HttpStatusCode.NotFound);
var client = new HttpClient(new FakeHandler() {Response = new HttpResponseMessage() {StatusCode = HttpStatusCode.NotFound}});
await client.FollowLinkAsync(link,machine);
Assert.True(notFoundHandler.NotFound);
}
示例6: FollowAndApplyInDistinctSteps
public async Task FollowAndApplyInDistinctSteps()
{
var link = new Link() {Target = new Uri("http://example.org/")};
var client = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.NotFound
} });
var clientState = new ClientApplicationState();
await client.FollowLinkAsync(link)
.ApplyRepresentationToAsync(clientState);
Assert.Equal(HttpStatusCode.NotFound, clientState.StatusCode);
}
示例7: Throws_exception_if_response_not_OK
public void Throws_exception_if_response_not_OK()
{
var response = new HttpResponseMessage(HttpStatusCode.BadRequest);
var httpClient = new HttpClient(new FakeHandler
{
Response = response,
InnerHandler = new HttpClientHandler()
});
var machine = new HttpResponseMachine();
machine.AddResponseHandler((l, r) => { throw new Exception(); }, HttpStatusCode.BadRequest);
var task = httpClient.FollowLinkAsync(new GuidServiceLink(),machine);
Assert.Throws<AggregateException>(() => task.Wait());
}
示例8: CodeSearchUsingLink
private static async Task CodeSearchUsingLink()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders
.UserAgent.Add(new ProductInfoHeaderValue("DarrelsGitClient", "1.0"));
// API Specific type acts as Request Factory
var codeSearchLink = new CodeSearchLink()
{
Template = new UriTemplate("https://api.github.com/search/code?q={query}{&page,per_page,sort_order}"),
Query = ".Result user:darrelmiller"
};
var requestMessage = codeSearchLink.CreateRequest();
var responseMessage = await httpClient.SendAsync(requestMessage);
// ReadAs extension takes advantage of media type defined by API
var githubdoc = await responseMessage.Content.ReadAsGithubDocumentAsync();
// API Specific Link Type knows how to interpret generic media type
var searchResults = CodeSearchLink.InterpretMessageBody(githubdoc);
foreach (var result in searchResults.Items)
{
Console.WriteLine(result.Path);
}
// Harvest strongly typed links from strongly typed searchresults
var fileLinks = searchResults.Items
.Where(i => i.Name.Contains("Slack") && i.CodeFileLink != null)
.Select(i => i.CodeFileLink);
foreach (var fileLink in fileLinks)
{
// Following Links is a way of navigating the API's object graph
var fileResponse = await httpClient.FollowLinkAsync(fileLink);
// Reuse media type parser
var fileDoc = await fileResponse.Content.ReadAsGithubDocumentAsync();
// Use Type Specific Link to interpret results
var codeFile = CodeFileLink.InterpretMessageBody(fileDoc);
Console.WriteLine(codeFile.Content);
}
}
示例9: SpecifyHandlerChainForAboutLink
public Task SpecifyHandlerChainForAboutLink()
{
var foo = false;
var bar = false;
var baz = false;
var registry = new LinkFactory();
var grh = new ActionResponseHandler((hrm) => baz = true,
new ActionResponseHandler((hrm) => foo = true,
new ActionResponseHandler((hrm) => bar = true)));
registry.SetHandler<AboutLink>(grh);
var link = registry.CreateLink<AboutLink>();
link.Target = new Uri("http://example.org");
var httpClient = new HttpClient(new FakeHandler() { Response = new HttpResponseMessage()});
return httpClient.FollowLinkAsync(link).ContinueWith(t =>
{
Assert.True(foo);
Assert.True(bar);
Assert.True(baz);
});
}
示例10: FollowLink
public async Task FollowLink()
{
var link = new Link { Target = new Uri("Http://localhost") };
var client = new HttpClient(new FakeMessageHandler());
var uri = string.Empty;
var machine = new HttpResponseMachine();
machine.When(HttpStatusCode.OK)
.Then(async (rel,r) => {uri = r.RequestMessage.RequestUri.AbsoluteUri; });
await client.FollowLinkAsync(link,machine);
Assert.Equal("http://localhost/", uri);
}
示例11: TypedLinksFromGenericType
private static async Task TypedLinksFromGenericType()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders
.UserAgent.Add(new ProductInfoHeaderValue("DarrelsGitClient", "1.0"));
// Factory for typed links
var linkFactory = new LinkFactory();
GitHubHelper.RegisterGitHubLinks(linkFactory);
// Get Home Document, without strongly typed object
var homelink = new HomeLink();
var homeResponse = await httpClient.FollowLinkAsync(homelink);
var githubDocument = await homeResponse.Content.ReadAsGithubDocumentAsync(linkFactory);
// Retrieve specific link from generic document
var codeSearchLink = githubDocument.GetLink<CodeSearchLink>();
codeSearchLink.Query = ".Result user:darrelmiller";
var responseMessage = await httpClient.FollowLinkAsync(codeSearchLink);
var githubDocument2 = await responseMessage.Content.ReadAsGithubDocumentAsync();
var searchResults = CodeSearchLink.InterpretMessageBody(githubDocument2);
// Show Results
foreach (var result in searchResults.Items)
{
Console.WriteLine(result.Path);
}
}
示例12: Returns_content_if_response_is_OK
public Task Returns_content_if_response_is_OK()
{
string content = Guid.NewGuid().ToString();
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(content);
var httpClient = new HttpClient(new FakeHandler
{
Response = response,
InnerHandler = new HttpClientHandler()
});
var link = new GuidServiceLink();
return httpClient.FollowLinkAsync(link).ContinueWith(
t => Assert.Equal(content, link.Guid));
}
示例13: FollowLink
public void FollowLink()
{
var link = new Link { Target = new Uri("Http://localhost") };
var client = new HttpClient(new FakeMessageHandler());
var uri = string.Empty;
link.HttpResponseHandler = new ActionResponseHandler(r => uri = r.RequestMessage.RequestUri.AbsoluteUri);
var task = client.FollowLinkAsync(link);
task.Wait();
Assert.Equal("http://localhost/", uri);
}