上传图片到第三方服务器

代码

/// <summary>
    /// 上传图片到第三方服务器
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="picNo"></param>
    /// <returns></returns>
    public string UploadFilesToBlueBox(string filePath, string picNo)
    {
        string strReturn = "";
        string fileName = Path.GetFileName(filePath);
        string strPostUrl = "http://pic.xxx.com/HttpPost_Upload.aspx?PICID=" + picNo + "&FILENAME=" + fileName;
        HttpWebRequest reqPost = (HttpWebRequest)WebRequest.Create(strPostUrl);
        reqPost.Method = "POST";
        reqPost.KeepAlive = false;
        reqPost.ContentType = "application/x-www-form-urlencoded";

        byte[] fileStream = AuthGetFileData(filePath);//把文件转为bute[],看之前的博客
        string strStream = Convert.ToBase64String(fileStream);
        byte[] strFiledata = Encoding.UTF8.GetBytes(strStream);
        reqPost.ContentLength = strFiledata.Length;
        //post数据
        using (Stream newStream = reqPost.GetRequestStream())
        {
            newStream.Write(strFiledata, 0, strFiledata.Length);
            //获取返回
            HttpWebResponse myResponse = (HttpWebResponse)reqPost.GetResponse();
            using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
            {
                //返回结果
                strReturn = reader.ReadToEnd();
            }
        }
        return strReturn;
    }

猜你喜欢

转载自www.cnblogs.com/xsj1989/p/9884752.html