aliexpress 上传图

首先,图片转化为字节流
        public byte[] ImagefileToByte(string srcImagePath)
        {
            System.IO.MemoryStream m = new System.IO.MemoryStream();
            System.Drawing.Bitmap bp = new System.Drawing.Bitmap(srcImagePath);
            bp.Save(m,System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] ImageByte = m.GetBuffer();
            return ImageByte;
        }


最外层调用的方法
        public string postUploadImage(string ImagePath)
        {
            string fileName = System.IO.Path.GetFileName(ImagePath);
            byte[] fileDatas = upImg.ImagefileToByte(ImagePath);
            Stream stream = new MemoryStream(fileDatas);
            string uploadImageResult = CreatePostHttpRespone(uploadImageUrl, stream, "application/octet-stream");
            return uploadImageResult;
            //uploadImageUrl=http://gw.api.alibaba.com/openapi/param2/1/aliexpress.open/api.uploadImage/自己店铺的appkey?fileName=aa.jpg&_aop_signature=签名
        }


       public  string CreatePostHttpRespone(string url,Stream stream,string contentType)
        {
                if (string.IsNullOrEmpty(url))
                {
                    throw new ArgumentNullException("url");
                }
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url) ;
                request.Method = "POST";
                request.Accept = "*/*";
                request.KeepAlive = true;
                request.ContentType = contentType;
                request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                request.ContentLength = stream.Length;
                using (Stream requestStream = request.GetRequestStream())
                {


                    //将内存流数据读取位置归零
                    stream.Position = 0;
                    byte[] tempBuffer = new byte[stream.Length];
                    stream.Read(tempBuffer, 0, tempBuffer.Length);
                    stream.Close();


                    //将内存流中的buffer写入到请求写入流
                    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                    requestStream.Close();


                    //获取到上传请求的响应
                    WebResponse response = request.GetResponse();


                    //获取到上传返回的数据
                    //string html = GetResponseString(response);
                    StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                    string content = reader.ReadToEnd();
                    response.Close();
                    request = null;


                    return content;
                }


            }

 

转载于:https://www.cnblogs.com/starluck/p/4184875.html

你可能感兴趣的:(aliexpress 上传图)