Upload raw byte data with dotnet core HttpClient - c#

I am updating some old C# code to use HttpClient instead of WebClient. As part of this, I need to upload a byte array of a file to an api.
With WebClient, this worked perfectly fine
byte[] data = GetMyData();
using (var client = new WebClient())
{
//set url, headers etc
var r = client.UploadData(url, "PUT", data);
}
With HttpClient, I've tried various methods, such as
byte[] data = GetMyData();
using (var client = new HttpClient())
{
//set url, headers etc
var r = await client.PutAsync(url, new ByteArrayContent(data));
}
I've also tried different ways of using Multipart data that I found Googling around, but the server does not accept anything I've tried. I don't have a lot of documentation on the server API, I only know that the WebClient way has worked well for many years. Is there a way to recreate the WebClient.UploadData behavior with HttpClient?

Thanks to the commenters for putting me on the right track. The Content-Type headers were not being set correctly for the HttpClient way, by putting it on the actual content. code below.
byte[] data = GetMyData();
using (var client = new HttpClient())
{
//set url, headers etc
var content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(contentType);
var r = await client.PutAsync(url, content);
}

Related

HTTP Rest Call using HttpClient in C# Get time out when byte array size becomes around 100 KB

I am calling to a rest api post method implement by me which has normal string parameters and one fileData byte array. I am just using application/json From Body technique to implement this service.
When i am calling to this api from C# code using HttpClient it is works fine for smaller files like 10-20 KBs. But when the file size is around 100-150 KBs PostAsync method is not getting anything untill the timeout happens. But when i call to same API from angular it is working fine for larger file sizes as well.
Below is my c# code block calling from client end. Please guide what is happening here.
Photo photo = new Photo
{
CustomerMobile = row["CustomerMobile"].ToString(),
PhotoCode = row["PhotoCode"].ToString(),
PhotoSize = row["PhotoSize"].ToString(),
TemplateId = int.Parse(row["TemplateId"].ToString()),
CreatedDate = DateTime.Parse(row["CreatedDate"].ToString()),
};
if (!string.IsNullOrEmpty(row["ExpiryDate"].ToString()))
photo.ExpiryDate = DateTime.Parse(row["ExpiryDate"].ToString());
var data = File.ReadAllBytes(row["PhotoUrl"].ToString());
photo.FileData = data;
photo.FileName = Path.GetFileName(row["PhotoUrl"].ToString());
using var client = new HttpClient();
HttpResponseMessage response = null;
client.BaseAddress = new Uri(Settings.Configurations.APIBaseServerURL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add(Settings.Configurations.Application, Settings.Configurations.ApplicationId);
client.DefaultRequestHeaders.Add(Settings.Configurations.SessionID, sessionID);
response = client.PostAsync(url,
new StringContent(JsonConvert.SerializeObject(photo).ToString(),
Encoding.UTF8, "application/json")).Result;

How do I upload document by converting HttpClient request to RestSharp request?

After i logged in to API i used, I am trying to upload a document with using RestSharp.
I was able to do this using Postman. Screenshot is given below:
Postman code generator generates this code block for me:
var client = new RestClient("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/pdf");
request.AddParameter("application/pdf", "<file contents here>", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
but idk how to edit the <file contents here> part from given code block above.
I was also able to achieve it using the HttpClient suggested in the respective API's documentation guide:
using (StreamReader sr = new StreamReader(#"C:\Users\fcomak\Downloads\sample.pdf"))
{
_httpClient.PutAsync("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", new StreamContent(sr.BaseStream))
.Result
.EnsureSuccessStatusCode();
}
But i need to convert HttpClient request to RestSharp request. I tried to achieve this using RestSharp, but failed. Even if I could send the file, its content was not browse correctly. Here is the code block i tried:
using (StreamReader sr = new StreamReader(#"C:\Users\fcomak\Downloads\sample.pdf"))
{
restClient.Execute(new RestRequest("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", Method.PUT)
.AddHeader("Content-Type", "application/pdf")
.AddParameter("application/pdf", new StreamContent(sr.BaseStream), ParameterType.RequestBody));
}
I can sending or getting json contents by using this api with this restClient by the way. And of course i logged in already. The reason for my failure is about my restClient.
I would be grateful for any help.
I just solved the problem.
using (WebClient wc = new WebClient())
{
byte[] value = wc.DownloadData("http://www.africau.edu/images/default/sample.pdf");
restClient.Execute(new RestRequest("http://80.211.238.187/AcumaticaERP/entity/Default/17.200.001/StockItem/POSTMAN123/files/sample.pdf", Method.PUT)
.AddHeader("Content-Type", "application/pdf")
.AddParameter("application/pdf", value, ParameterType.RequestBody)
);
}
As you see, i used WebClient.DownloadData() instead of StreamReader.BaseStream()
Or, you can convert you local data to byte array with File.ReadAllBytes()
string entitySource = #"C:\Users\fcomak\Downloads\sample2.pdf";
byte[] value = File.ReadAllBytes(entitySource);
restClient.Execute(new RestRequest(entityBasePath + entityName + "/" + inventoryID + "/files/" + fileName, Method.PUT)
.AddHeader("Content-Type", "application/pdf")
.AddParameter("application/pdf", value, ParameterType.RequestBody)
);
FYI

Trouble making POST request from c# [.NET]

We have a created an API for the application which takes the image via POST request process it and sends the result in JSON format.
We tried calling API from different sources like python, postman app, c#. We can successfully call end point using python and postman app but with c# getting error
c# code [Not working]
byte[] img_data = System.IO.File.ReadAllBytes(#"file_path");
string url_ep = "http://ip:port/get";
Dictionary<string, byte[]> fl_image = new Dictionary<string, byte[]>();
fl_image.Add("image", img_data);
string data = JsonConvert.SerializeObject(fl_image);
var dataToSend = Encoding.UTF8.GetBytes(data);
var request = HttpWebRequest.Create(url_ep);
request.ContentType = "application/json";
request.ContentLength = dataToSend.Length;
request.Method = "POST";
request.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = request.GetResponse();
System.IO.Stream dataStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
python code [working]
import requests
url = 'http://ip:port/get'
fl_image = {'image': open('file_path', 'rb')}
res = requests.post(url, files=fl_image)
print(res.json())
API Endpoint
from flask import Flask, request
import numpy as np
import cv2 as cv
#app.route('/get', methods = ['POST'])
def get_image():
if request.method == 'POST':
file = request.files['image']
# Read file
f = file.read()
# convert string of image data to uint8
f1 = np.fromstring(f, np.uint8)
# decode image
f2 = cv.imdecode(f1,cv.IMREAD_COLOR)
There are several issues with the way you are posting data from C#. The most relevant one is that you are trying to post a file as a JSON object, with file contents as string.
This cannot work: your python server is clearly expecting multipart/form-data as content-type.
I also strongly recommend you to use HttpClient and not the old HttpWebRequest class to send HTTP Requests.
var filePath = #"file_path";
var url = "http://ip:port/get";
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
using (var fileStream = File.OpenRead(filePath))
{
var imageContent = new StreamContent(fileStream);
// NOTE: the line below is not required, but useful when you know the media type
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(imageContent, "image", Path.GetFileName(filePath));
var response = await client.PostAsync(url, content);
var stringResponse = await response.Content.ReadAsStringAsync();
// do what you need with the response
}
Other minor issues:
Do not read the entire file in memory (using File.ReadAllBytes), but open a stream for reading instead.
Use async/await when possible, do not block on async code (do not use .Result, .Wait() or .GetAwaiter().GetResult() on Task or Task<T>)
Always call Dispose() on IDisposable objects when you have finished using them (wrapping them inside a using block)
You need to dispose the connections
reader.Close();
dataStream.Close();
response.Close();
Hope this helps
Or try using HttpClient for .net within the using block

.NETCore HttpWebRequest - Old Way isn't Working

Before I upgraded to the newest .NetCore I was able to run the HttpWebRequest, add the headers and content Type and pull the stream of the JSON file from Twitch. Since the upgrade this is not working. I receive a Web Exception each time I go to get the response Stream. Nothing has changed with twitch because it still works with the old Bot. The old code is below:
private const string Url = "https://api.twitch.tv/kraken/streams/channelname";
HttpWebRequest request;
try
{
request = (HttpWebRequest)WebRequest.Create(Url);
}
request.Method = "Get";
request.Timeout = 12000;
request.ContentType = "application/vnd.twitchtv.v5+json";
request.Headers.Add("Client-ID", "ID");
try
{
using (var s = request.GetResponse().GetResponseStream())
{
if (s != null)
using (var sr = new StreamReader(s))
{
}
}
}
I have done some research and found that I may need to start using either an HttpClient or HttpRequestMessage. I have tried going about this but when adding headers content type the program halts and exits. after the first line here: (when using HttpsRequestMessage)
request.Content.Headers.ContentType.MediaType = "application/vnd.twitchtv.v5+json";
request.Content.Headers.Add("Client-ID", "rbp1au0xk85ej6wac9b8s1a1amlsi5");
You are trying to add a ContentType header, but what you really want is to add an Accept header (your request is a GET and ContentType is used only on requests which contain a body, e.g. POST or PUT).
In .NET Core you need to use HttpClient, but remember that to correctly use it you need to leverage the use of async and await.
Here it is an example:
using System.Net.Http;
using System.Net.Http.Headers;
private const string Url = "https://api.twitch.tv/kraken/streams/channelname";
public static async Task<string> GetResponseFromTwitch()
{
using(var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v5+json"));
client.DefaultRequestHeaders.Add("Client-ID", "MyId");
using(var response = await client.GetAsync(Url))
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync(); // here we return the json response, you may parse it
}
}
}

Streaming Content constantly updating 8x8 streaming service

I have tried to create a simple console application.
We have a call system from 8x8 that provide a web streaming API but their documentation is very limited and nothing in C#.
The api service streams call statuses in near real time and I would like to get that 'stream' and be able to read and process it in realtime if possible. The response or Content Type is 'text/html'. But the actual body of the response can be declared as json - sample below:
{"Interaction":{"attachedData":{"attachedDatum":[{"attachedDataKey":"#pri","attachedDataValue":100},{"attachedDataKey":"callingName","attachedDataValue":999999999999},{"attachedDataKey":"cha","attachedDataValue":99999999999},{"attachedDataKey":"cnt","attachedDataValue":0},{"attachedDataKey":"con","attachedDataValue":0},{"attachedDataKey":"med","attachedDataValue":"T"},{"attachedDataKey":"pho","attachedDataValue":9999999999},{"attachedDataKey":"phoneNum","attachedDataValue":9999999999},{"attachedDataKey":"tok","attachedDataValue":999999999}]},"event":"InteractionCreated","inboundChannelid":9999999999,"interactionEventTS":9999999,"interactionGUID":"int-15b875d0da2-DJOJkDhDsrh3AIaFP8VkICv9t-phone-01-testist","resourceType":0}}
I have seen several posts concerning httpClient and the GetAsync methods but none of these appear to work as they appear to be for calls when a response is made, not something that constantly has a response.
Using fiddler for the call it does not appear to close so the stream is constantly running, so fiddler does not display any data until a separate user or instance connects.
When I use a browser the content is 'streamed' to the page and updates automatically and shows all the content (as above).
The api contains authentication so when another client connects and retrieves data the connected client closes and finally I am able to see the data that was gathering.
This is the code so and does return the big stream when another client connects but ideally I want a real time response and appears to just get stuck in the GETASYNC method:
var response = await client.GetAsync(address, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = await responseContent.ReadAsStringAsync();
Console.WriteLine(responseString);
}
Hopefully that's enough information for one of you clever people to help me in my predicament.
I was also having an issue consuming their streaming API and the examples I found that worked with the Twitter and CouchBase streaming API's did not work with 8x8. Both Twitter and CouchBase send line terminators in their pushes so the solution relied on ReadLine to pull in the feed. Since 8x8 does not send terminators you'll need to use ReadBlock or better ReadBlockAsync.
The following code shows how to connect using credentials and consume their feed:
private static async Task StreamAsync(string url, string username, string password)
{
var handler = new HttpClientHandler()
{
Credentials = new NetworkCredential {UserName = username, Password = password},
PreAuthenticate = true
};
// Client can also be singleton
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Connection.Add("keep-alive");
using (var response = await client.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead))
{
using (var body = await response.Content.ReadAsStreamAsync())
{
using (var reader = new StreamReader(body))
{
while (!reader.EndOfStream)
{
var buffer = new char[1024];
await reader.ReadBlockAsync(buffer, 0, buffer.Length);
Console.WriteLine(new string(buffer));
}
}
}
}
}
}

Categories

Resources