'How to make an HttpClient call from c# to get the result from the below code
The below is a webservice running, I can use swagger and get the result file from apicontroller but having trouble making the call from console app to get the results. What would a HttpClient call look like to get the results using c#.
[HttpGet, Route("api/DownloadHl7/{securitykey}/{specimenid}")]
public IHttpActionResult GetFileForCustomer(string securitykey, string specimenid) {
if (securitykey != Constants.ApiToken)
return BadRequest();
var file = FileToByteArray(pathtohl7 + specimenid + ".HL7");
IHttpActionResult response;
HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK);
responseMsg.Content = new ByteArrayContent(file);
responseMsg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/HL7");
response = ResponseMessage(responseMsg);
return response;
}
public byte[] FileToByteArray(string fileName) {
byte[] fileData = null;
using (FileStream fs = File.OpenRead(fileName)) {
using (BinaryReader binaryReader = new BinaryReader(fs)) {
fileData = binaryReader.ReadBytes((int)fs.Length);
}
}
return fileData;
}
Solution 1:[1]
Finally got it to work using the below, There was no need to change apicontroller code, worked as is.
This will download the file to the local directory indicated in the correct format.
using (HttpClient client = new HttpClient()) {
var response = await client.GetStreamAsync(downloadurlurl + "securitykey" + "/" + "741562");
using (var fs = new FileStream(string.Format(@"C:\Bill\{0}.HL7", "741562"),
FileMode.CreateNew)) {
await response.CopyToAsync(fs);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | khaled Dehia |
