本文整理汇总了C#中System.Net.Http.HttpClient.PostAsXmlAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.PostAsXmlAsync方法的具体用法?C# HttpClient.PostAsXmlAsync怎么用?C# HttpClient.PostAsXmlAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.PostAsXmlAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PostOrderAsync
/// <summary>
/// Save order on web api.
/// </summary>
/// <param name="newOrder">New Order to be saved.</param>
/// <returns>Tuple of bool and string, bool == true when API succeded, bool == false when API did not succeed, string == fail message.</returns>
public Tuple<bool,string> PostOrderAsync(NewOrderInfo newOrder)
{
using (var client = new HttpClient())
{
try
{
client.BaseAddress = _baseAddress;
var response = client.PostAsXmlAsync("api/order/create", newOrder, new CancellationToken()).Result;
if (response.IsSuccessStatusCode)
{
var answer = new Tuple<bool, string>(true, response.StatusCode.ToString());
return answer;
}
else //do failure thing
{
var answer = new Tuple<bool, string>(false, "Could not save the created order: Error from Web api: " + response.StatusCode.ToString() + ": " + response.ReasonPhrase);
return answer;
}
}
catch (Exception ex)
{
throw;
}
}
}
示例2: ProcessPostRequests
internal static async void ProcessPostRequests(HttpClient client)
{
ResponseAlbumModel album = new ResponseAlbumModel
{
Title = "Xml added",
ProducerName = "Xml Produer",
Year = 2000
};
var httpResponse = await client.PostAsXmlAsync("api/albums", album);
Console.WriteLine(httpResponse.ReasonPhrase.ToString() + " from albums");
}
示例3: AddSongXml
public static void AddSongXml(HttpClient client)
{
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/xml"));
Console.WriteLine("Enter song title");
string title = Console.ReadLine();
Console.WriteLine("Enter song year");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("Enter song genre code");
Genre genre = (Genre)int.Parse(Console.ReadLine());
Console.WriteLine("Enter song description");
string description = Console.ReadLine();
Console.WriteLine("Enter song artist id");
int id = int.Parse(Console.ReadLine());
Song song = new Song { SongTitle = title, SongYear = year, SongGenre = genre, Description = description, ArtistId = id };
HttpResponseMessage response =
client.PostAsXmlAsync("api/songs", song).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Song added");
}
else
{
Console.WriteLine("{0} ({1})",
(int)response.StatusCode, response.ReasonPhrase);
}
}
示例4: AddArtistXml
public static void AddArtistXml(HttpClient client)
{
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/xml"));
Console.WriteLine("Enter artist name");
string name = Console.ReadLine();
Console.WriteLine("Enter artist country");
string country = Console.ReadLine();
Console.WriteLine("Enter artist birth date");
DateTime birthDate = DateTime.ParseExact(Console.ReadLine(), "dd.MM.yyyy", new CultureInfo("en-US"));
Artist artist = new Artist { Name = name, Country = country, DateOfBirth = birthDate };
HttpResponseMessage response =
client.PostAsXmlAsync("api/artists", artist).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Artist added");
}
else
{
Console.WriteLine("{0} ({1})",
(int)response.StatusCode, response.ReasonPhrase);
}
}
示例5: AddAlbumXml
public static void AddAlbumXml(HttpClient client)
{
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/xml"));
Console.WriteLine("Enter album title");
string title = Console.ReadLine();
Console.WriteLine("Enter album year");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("Enter producer");
string producer = Console.ReadLine();
Album album = new Album { AlbumTitle = title, AlbumYear = year, Producer = producer };
HttpResponseMessage response =
client.PostAsXmlAsync("api/albums", album).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Album added");
}
else
{
Console.WriteLine("{0} ({1})",
(int)response.StatusCode, response.ReasonPhrase);
}
}
示例6: PostCanRespondInXml
public async void PostCanRespondInXml()
{
var message = new MessageDto
{
Text = "This is XML"
};
var client = new HttpClient(_server);
var response = await client.PostAsXmlAsync(Url + "hello", message);
var result = await response.Content.ReadAsAsync<MessageDto>(new [] {new XmlMediaTypeFormatter() });
Assert.Equal(message.Text, result.Text);
}
示例7: NotifySites
public static async Task NotifySites()
{
BlogConfigurationDto configuration = configurationDataService.GetConfiguration();
IEnumerable<Uri> pingSites = pingDataService.GetPingSites();
foreach (Uri pingSite in pingSites)
{
using (HttpClient client = new HttpClient())
{
await client.PostAsXmlAsync(pingSite.ToString(), GetRequest(pingSite, configuration));
}
}
}
示例8: AddNewSongAsXML
internal static Song AddNewSongAsXML(HttpClient Client, string title, int year, string genre, Artist artist)
{
var album = new Song() { Title = title, Year = year, Genre = genre, Artist = artist };
var response = Client.PostAsXmlAsync("api/Songs", album).Result;
Song resultSong = response.Content.ReadAsAsync<Song>().Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Song added!");
return resultSong;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
return new Song();
}
示例9: AddNewArtistAsXml
internal static Artist AddNewArtistAsXml(HttpClient Client, string name, string country, DateTime dateOfBirth)
{
var artist = new Artist { Name = name, Country = country, DateOfBirth = dateOfBirth };
var response = Client.PostAsXmlAsync("api/Artists", artist).Result;
Artist resultArtist = response.Content.ReadAsAsync<Artist>().Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Artist added!");
return resultArtist;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
return new Artist();
}
示例10: AddNewAlbumAsXml
internal static Album AddNewAlbumAsXml(HttpClient Client, string title, int year, string producer, ICollection<Song> songs, ICollection<Artist> artists)
{
var album = new Album() { Title = title, Year = year, Producer = producer, Songs = songs, Artists = artists };
var response = Client.PostAsXmlAsync("api/Albums", album).Result;
Album resultAlbum = response.Content.ReadAsAsync<Album>().Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Album added!");
return resultAlbum;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
return new Album();
}
示例11: AddSong
public static void AddSong(HttpClient client, Song song)
{
HttpResponseMessage response;
if (client.DefaultRequestHeaders.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/json")))
{
response = client.PostAsJsonAsync("api/songs", song).Result;
}
else
{
response = client.PostAsXmlAsync("api/songs", song).Result;
}
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Artist {0} added successfully", song.Title);
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
示例12: Notify
public static async Task Notify(ItemDto item, Uri uri)
{
SiteUrl itemUrl = item is PostDto
? urlBuilder.Post.Permalink(item)
: urlBuilder.Page.Permalink(item);
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(uri);
KeyValuePair<string, IEnumerable<string>> header = response.Headers.SingleOrDefault(h => h.Key.Equals("x-pingback", StringComparison.OrdinalIgnoreCase) || h.Key.Equals("pingback", StringComparison.OrdinalIgnoreCase));
string urlToPing = header.Value.FirstOrDefault();
if (string.IsNullOrEmpty(urlToPing))
{
return;
}
var xmlRequest = GetXmlRequest(itemUrl, uri);
await client.PostAsXmlAsync(urlToPing, xmlRequest);
}
}
示例13: Add
public static async void Add(HttpClient httpClient, SongApiModel song)
{
var response = await httpClient.PostAsXmlAsync("song", song);
Console.WriteLine("Done!");
}
示例14: Main
static void Main(string[] args)
{
// Prepare for a simple HttpPost request
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:61924/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
// Get the body of the request
var vMRRequest = GetSampleRequest();
// Construct the DSS-level request, base-64 encoding the vMR request within it.
var evaluate =
new evaluate
{
interactionId =
new InteractionIdentifier
{
interactionId = Guid.NewGuid().ToString("N"),
scopingEntityId = "SAMPLE-CLIENT",
submissionTime = DateTime.Now.ToUniversalTime()
},
evaluationRequest =
new EvaluationRequest
{
clientLanguage = "XXX",
clientTimeZoneOffset = "XXX",
dataRequirementItemData = new List<DataRequirementItemData>
{
new DataRequirementItemData
{
driId = new ItemIdentifier { itemId = "RequiredDataId" },
data =
new SemanticPayload
{
informationModelSSId = SemanticSignifiers.CDSInputId,
base64EncodedPayload = Packager.EncodeRequestPayload(vMRRequest)
}
}
},
kmEvaluationRequest = new List<KMEvaluationRequest>
{
new KMEvaluationRequest { kmId = new EntityIdentifier { scopingEntityId = "org.hl7.cds", businessId = "NQF-0068", version = "1.0" } }
}
}
};
// Post the request and retrieve the response
var response = client.PostAsXmlAsync("api/evaluation", evaluate).Result;
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Evaluation succeeded.");
var evaluateResponse = response.Content.ReadAsAsync<evaluateResponse>().Result;
var evaluationResponse = evaluateResponse.evaluationResponse.finalKMEvaluationResponse.First();
if (evaluationResponse.kmId.scopingEntityId != "SAMPLE-CLIENT")
{
Console.WriteLine("Evaluation did not return the input scoping entity Id.");
}
var evaluationResult = evaluationResponse.kmEvaluationResultData.First();
if (!SemanticSignifiers.AreEqual(evaluationResult.data.informationModelSSId, SemanticSignifiers.CDSActionGroupResponseId))
{
Console.WriteLine("Evaluation did not return an action group response.");
}
var actionGroupResponse = Packager.DecodeActionGroupResponsePayload(evaluationResult.data.base64EncodedPayload);
var createAction = actionGroupResponse.actionGroup.subElements.Items.First() as CreateAction;
if (createAction == null)
{
Console.WriteLine("Result does not include a CreateAction.");
}
else
{
var proposalLiteral = createAction.actionSentence as elm.Instance;
if (proposalLiteral == null)
{
Console.WriteLine("Resulting CreateAction does not have an ELM Instance as the Action Sentence.");
}
else
{
if (proposalLiteral.classType.Name != "SubstanceAdministrationProposal")
{
Console.WriteLine("Resulting proposal is not a substance administration proposal");
}
else
{
Console.WriteLine("Substance Administration Proposed: {0}.", (proposalLiteral.element.Single(e => e.name == "substanceAdministrationGeneralPurpose").value as elm.Code).display);
}
}
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
//.........这里部分代码省略.........
示例15: When_POST_typed_XML_content_can_be_read
public void When_POST_typed_XML_content_can_be_read()
{
var reqModel = new SomeModel
{
AnInteger = 2,
AString = "hello"
};
var client = new HttpClient();
client.PostAsXmlAsync(_baseAddress + "Test", reqModel);
TestController.OnPost(req =>
{
var recModel = req.Content.ReadAsAsync<SomeModel>().Result;
Assert.Equal(reqModel.AnInteger, recModel.AnInteger);
Assert.Equal(reqModel.AString, recModel.AString);
return new HttpResponseMessage();
});
}