Home > OS >  System.Net.Http.Handlers missing unity
System.Net.Http.Handlers missing unity

Time:03-04

Net.Http.Handlers namespace but I Got : The type or namespace name 'Handlers' does not exist in the namespace 'System.Net.Http' (are you missing an assembly reference?)

visual studio Add reference is not working cause unity download System.Net.Http dll and put it to unity dll's not working too update System.Net.Http from nuget but The problem has not been solved yet

My current code is:

public static async Task<string> Upload(string uri, string pathFile)
    {

        byte[] bytes = System.IO.File.ReadAllBytes(pathFile);

        using (var content = new ByteArrayContent(bytes))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("*/*");

            //Send it
            var response = await nftClient.PostAsync(uri, content);
            response.EnsureSuccessStatusCode();
            Stream responseStream = await response.Content.ReadAsStreamAsync();
            StreamReader reader = new StreamReader(responseStream);
            return reader.ReadToEnd();
        }
    }

CodePudding user response:

I would just use UnityWebRequest and use uploadProgress to update the progress:

UnityWebRequest uwr;

void Start()
{
    StartCoroutine(PutRequest("http:///www.yoururl.com"));
    StartCoroutine(UploadProgressCoroutine());
}

IEnumerator PutRequest(string url)
{
    byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test");
    uwr = UnityWebRequest.Put(url, dataToPut);
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: "   uwr.error);
    }
    else
    {
        Debug.Log("Received: "   uwr.downloadHandler.text);
    }
}

IEnumerator UploadProgressCoroutine()
{
    while (!uwr.isDone)
    {
        HandleProgress(uwr.uploadProgress);
        yield return null;
    }
}

void HandleProgress(float currentProgress)
{
    // currentProgress is value between 0 and 1
}

Based on Programmer's code here.

Or for your specific use case, you will need to create your own UnityWebRequest to get the headers and data the way your server expects:

UnityWebRequest uwr;

void Start()
{
    StartCoroutine(PostRequest("http:///www.yoururl.com", "/file/path/here"));
    StartCoroutine(UploadProgressCoroutine());
}

IEnumerator PostRequest(string url, string filePath)
{
    byte[] dataToPost = System.IO.File.ReadAllBytes(filePath);
    UploadHandlerRaw uhr = new UploadHandlerRaw(dataToPost);

    uwr = new UnityWebRequest(url, "POST", new DownloadHandlerBuffer(), uhr);
    uwr.SetRequestHeader("Content-Type", "*/*");
    uwr.SetRequestHeader("Accept", "application/json"); 
    uwr.SetRequestHeader("Authorization", "Bearer "   apiToken);

    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: "   uwr.error);
    }
    else
    {
        Debug.Log("Received: "   uwr.downloadHandler.text);
    }
}

IEnumerator UploadProgressCoroutine()
{
    while (!uwr.isDone)
    {
        HandleProgress(uwr.uploadProgress);
        yield return null;
    }
}

void HandleProgress(float currentProgress)
{
    // currentProgress is value between 0 and 1
}
  • Related