WebClient set headers

后端 未结 4 961
栀梦
栀梦 2021-01-07 16:34

How I can set a header in the webClient class? I tried:

client.Headers[\"Content-Type\"] = \"image/jpeg\";

that throws a

4条回答
  •  别那么骄傲
    2021-01-07 16:48

    If the header already exists:

    client.Headers.Set("Content-Type", "image/jpeg");
    

    if its a new header:

    client.Headers.Add("Content-Type", "image/jpeg");
    

    Also, there is a chance that you are getting an error because you are trying to set the headers too late. Post your exception so we can let you know.

    Update

    Looks like there are some weird restrictions on the "Content-Type" header with the WebClient class. Look in to using the client.Download methods (DownloadData, DownloadFile, etc...)

    See if using the "UploadFile" method on webclient works rather than doing it manually. It returns the respose body byte[].

    If you continue to have issues with the WebClient, try justing using a plain old HttpRequest/HttpWebRequest.

提交回复
热议问题