免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
無法在c#中使用httpwebrequest上傳圖片

我試圖通過API以編程方式將圖像上傳到另一臺服務(wù)器. API希望我在一個字節(jié)數(shù)組中上傳圖像,以便在字段中發(fā)送:“image_content”.

我的實現(xiàn)和調(diào)用代碼如下. Web請求命中服務(wù)器,但服務(wù)器響應(yīng)我的Web請求中不存在該圖像.

當(dāng)我運行以下代碼時,我收到的錯誤是圖像不在請求中.我在這里錯過了什么?

public static class FormUpload{    private static readonly Encoding encoding = Encoding.UTF8;    public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)    {        string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());        string contentType = "multipart/form-data; boundary="   formDataBoundary;    byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);    return PostForm(postUrl, userAgent, contentType, formData);}private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData){    HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;    if (request == null)    {        throw new NullReferenceException("request is not a http request");    }    // Set up the request properties.    request.Method = "POST";    request.ContentType = contentType;    request.UserAgent = userAgent;    request.ContentLength = formData.Length;    // Send the form data to the request.    using (Stream requestStream = request.GetRequestStream())    {        requestStream.Write(formData, 0, formData.Length);        requestStream.Close();    }    return request.GetResponse() as HttpWebResponse;}private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary){    Stream formDataStream = new System.IO.MemoryStream();    bool needsCLRF = false;    foreach (var param in postParameters)    {         if (param.Value is FileParameter)        {            FileParameter fileToUpload = (FileParameter)param.Value;            // Add just the first part of this param, since we will write the file data directly to the Stream            string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",                boundary,                param.Key,                fileToUpload.FileName ?? param.Key,                fileToUpload.ContentType ?? "application/octet-stream");            formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));            // Write the file data directly to the Stream, rather than serializing it to a string.            formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);        }        else        {            string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",                boundary,                param.Key,                param.Value);            formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));        }    }    // Add the end of the request.  Start with a newline    string footer = "\r\n--"   boundary   "--\r\n";    formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));    // Dump the Stream into a byte[]    formDataStream.Position = 0;    byte[] formData = new byte[formDataStream.Length];    formDataStream.Read(formData, 0, formData.Length);    formDataStream.Close();    return formData;}public class FileParameter{    public byte[] File { get; set; }    public string FileName { get; set; }    public string ContentType { get; set; }    public FileParameter(byte[] file) : this(file, null) { }    public FileParameter(byte[] file, string filename) : this(file, filename, null) { }    public FileParameter(byte[] file, string filename, string contenttype)    {        File = file;        FileName = filename;        ContentType = contenttype;    }}

}

調(diào)用上述函數(shù)的代碼是:

// Read file dataFileStream fs = new FileStream("c:\\myimage.jpeg", FileMode.Open, FileAccess.Read);byte[] data = new byte[fs.Length];fs.Read(data, 0, data.Length);fs.Close();// Generate post objectsDictionary<string, object> postParameters = new Dictionary<string, object>();postParameters.Add("image_content",data);// Create request and receive responsestring postURL = "myurl";string userAgent = "Mozilla";HttpWebResponse webResponse = FormUpload.MultipartFormDataPost(postURL, userAgent, postParameters);// Process responseStreamReader responseReader = new StreamReader(webResponse.GetResponseStream());string fullResponse = responseReader.ReadToEnd();webResponse.Close();Response.Write(fullResponse);

解決方法:

在我看來,你應(yīng)該使用MultipartFormDataContent 類,因為它“為使用multipart / form-data MIME類型編碼的內(nèi)容提供容器.”.試試這個

    public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, byte[] data)    {        string contentType;        byte[] formData = Program.GetMultipartFormData(data, out contentType);          return PostForm(postUrl, userAgent, contentType, formData);    }    public static byte[] GetMultipartFormData(byte[] data, out string contentType)    {        var byteArrayContent = new ByteArrayContent(data);        byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");        byteArrayContent.Headers.Add("image_content", "myimage.jpeg");        var content = new MultipartFormDataContent(String.Format("----------{0:N}", Guid.NewGuid())) { byteArrayContent };        contentType = content.Headers.ContentType.ToString();        return content.ReadAsByteArrayAsync().Result;    }
來源:http://www.icode9.com/content-1-212001.html
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
文件的上傳(表單上傳和ajax文件異步上傳)
C# POST 提交文件和文本數(shù)據(jù)
用 WebClient.UploadData 方法 上載文件數(shù)據(jù)
通過IdHTTP自動提交網(wǎng)頁信息
關(guān)于urlDecode和tomcat一些源碼
android 將int轉(zhuǎn)byte,byte轉(zhuǎn)int的兩種方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服