示例#1
0
文件: QQHelper.cs 项目: baixue001/IPS
        public string AddBlog(string msg, string imgurl)
        {
            string     authUrl = BlogAuthUrl + "add_pic_t";
            HttpHelper http    = new HttpHelper();
            Dictionary <string, object> param = new Dictionary <string, object>();

            param.Add("access_token", Token);
            param.Add("oauth_consumer_key", PlatConfig.QQKey);
            param.Add("openid", OpenID);
            param.Add("content", msg);
            if (!string.IsNullOrEmpty(imgurl))
            {
                M_Http_File file = new M_Http_File(GetFileByte(imgurl), Path.GetFileName(imgurl), M_Http_File.GetImgContentType(imgurl));
                param.Add("pic", file);
            }
            //HttpResult result = http.UploadParam(authUrl, param);
            //return result.Html;
            M_Http_Result result = HttpHelper.Post(new M_Http_Request()
            {
                PostUrl   = authUrl,
                formDatas = param
            });

            return(result.resultStr);
        }
示例#2
0
        private static byte[] GetMultipartFormData(Dictionary <string, object> postParameters, string boundary)
        {
            Stream formDataStream = new MemoryStream();
            bool   needsCLRF      = false;

            var DefaultEncoding = Encoding.UTF8;

            foreach (var param in postParameters)
            {
                if (needsCLRF)
                {
                    formDataStream.Write(DefaultEncoding.GetBytes("\r\n"), 0, DefaultEncoding.GetByteCount("\r\n"));
                }
                needsCLRF = true;
                if (param.Value is M_Http_File)
                {
                    M_Http_File fileToUpload = (M_Http_File)param.Value;

                    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, // param.Key, //此处如果是请求的php,则需要约定好 存取一致 php:$_FILES['img']['name']
                                                  fileToUpload.FileName ?? param.Key,
                                                  fileToUpload.ContentType ?? "application/octet-stream");

                    // 将与文件相关的header数据写到stream中
                    formDataStream.Write(DefaultEncoding.GetBytes(header), 0, DefaultEncoding.GetByteCount(header));
                    // 将文件数据直接写到stream中
                    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(DefaultEncoding.GetBytes(postData), 0, DefaultEncoding.GetByteCount(postData));
                }
            }

            string tailEnd = "\r\n--" + boundary + "--\r\n";

            formDataStream.Write(DefaultEncoding.GetBytes(tailEnd), 0, DefaultEncoding.GetByteCount(tailEnd));

            // 将Stream数据转换为byte[]格式
            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();

            return(formData);
        }
示例#3
0
        //public HttpConfig config = new HttpConfig();
        #region 文件上传 同步/异步 OLD
        /// <summary>
        /// 单文件上传,用于微信等异步上传文件后返回资源ID
        /// </summary>
        /// <param name="url">服务器URL</param>
        /// <param name="name">元素名称=Request.Files[name]</param>
        /// <param name="fileMod">文件信息(文件名称,字节,ContentType)</param>
        /// <param name="callback">文件上传统一异步,必须实现回调</param>
        public static M_Http_Request UploadSignleFile(string url, string name, M_Http_File fileMod, M_Http_Request.ResponseCallback callback = null)
        {
            M_Http_Request req = new M_Http_Request()
            {
                PostUrl = url
            };

            req.formDatas.Add(name, fileMod);
            req.callback = callback;
            if (req.callback != null)
            {
                req.IsAsync = true;
            }
            HttpHelper.PostWithFile(req);
            return(req);
        }