我想通过向API发送json数据来获取图像。我使用WebClient下载图片。
string URI = "http://my_api.com";
string myParameters = "{'my': 'json_object'}";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}UploadString方法的返回类型为string。如何将此结果转换为ContentType: image/jpeg?
发布于 2015-02-28 00:09:18
要将字符串转换为jpg,您需要将字符串转换为byte[]...但是我想问为什么不使用WebClient.DownloadFile呢?
using (WebClient w = new WebClient())
{
//add user agent string
w.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
//download to disk
w.DownloadFile(uri, savePath);
}我还在报头中添加了一个用户代理字符串,指示Google Chrome,因为一些服务器会基于此来阻止请求,并返回HTTP禁止错误。
https://stackoverflow.com/questions/28768200
复制相似问题