Guest User

Untitled

a guest
Oct 25th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. async Task<string> DoCamCardAsync()
  2. {
  3. var cli = new HttpClient();
  4. cli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  5.  
  6. // Why use a custom boundary for multipart/form-data?
  7. // .NET sticks the thing in quotes in the header; the CamCard server goes nuts for the quotes (takes them literally); while the
  8. // actual boundary is delineated in the request by the unquoted boundary.
  9. var boundary = Guid.NewGuid().ToString();
  10. using (var form = new MultipartFormDataContent(boundary))
  11. {
  12. form.Headers.Remove("Content-Type");
  13. form.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
  14.  
  15. byte[] cardBytes = File.ReadAllBytes(@"file path here, or use stream instead");
  16. var fileContent = new ByteArrayContent(cardBytes);
  17. fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
  18. {
  19. Name=""upfile"",
  20. FileName = ""card_m.jpg""
  21. };
  22. fileContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("image/jpeg");
  23. form.Add(fileContent);
  24.  
  25. var sURl = "https://bcr1.intsig.net/BCRService/BCR_VCF2?user=[your username here]&pass=[your key here]&json=1&lang=1";
  26. var response = await cli.PostAsync(sURl, form);
  27. return await response.Content.ReadAsStringAsync();
  28. }
Add Comment
Please, Sign In to add comment