Exemplo n.º 1
0
        /// <summary>
        /// 发起get请求,以字符串返回
        /// </summary>
        /// <param name="methodOrUri"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string getForString(string methodOrUri, YopRequest request)
        {
            //String serverUrl = buildURL(methodOrUri, request);
            //request.setAbsoluteURL(serverUrl);
            //String content = getRestTemplate(request).getForObject(serverUrl, String.class);
            //  if (logger.isDebugEnabled()) {
            //      logger.debug("response:\n" + content);
            //  }
            string serverUrl = richRequest(HttpMethodType.GET, methodOrUri,
                                           request);

            signAndEncrypt(request);
            request.setAbsoluteURL(serverUrl);
            request.encoding("");
            //请求网站

            Stream stream  = HttpUtils.PostAndGetHttpWebResponse(request, "GET").GetResponseStream();
            string content = new StreamReader(stream, Encoding.UTF8).ReadToEnd();

            return(content);
        }
Exemplo n.º 2
0
        public static string uploadForString(string methodOrUri, YopRequest request)
        {
            string serverUrl = richRequest(HttpMethodType.POST, methodOrUri, request);

            //NameValueCollection original = request.getParams();
            //NameValueCollection alternate = new NameValueCollection();
            List <string> uploadFiles = request.getParam("fileURI");

            if (null == uploadFiles || uploadFiles.Count == 0)
            {
                throw new SystemException("上传文件时参数_file不能为空!");
            }

            List <UploadFile> upfiles = new List <UploadFile>();

            foreach (string strPath in uploadFiles)
            {
                UploadFile files = new UploadFile();
                files.Name     = "_file";
                files.Filename = Path.GetFileName(strPath);

                Stream oStream  = File.OpenRead(strPath.Split(':')[1]);
                byte[] arrBytes = new byte[oStream.Length];
                int    offset   = 0;
                while (offset < arrBytes.LongLength)
                {
                    offset += oStream.Read(arrBytes, offset, arrBytes.Length - offset);
                }
                files.Data = arrBytes;
                upfiles.Add(files);
            }

            signAndEncrypt(request);
            request.setAbsoluteURL(serverUrl);
            request.encoding("blowfish");

            Stream stream  = HttpUtils.PostFile(request, upfiles).GetResponseStream();
            string content = new StreamReader(stream, Encoding.UTF8).ReadToEnd();

            return(content);


            //foreach(string uploadFile in uploadFiles)
            //{
            //  try
            //  {
            //    alternate.Add("fileURI", new UrlResource(new URI(uploadFile)));

            //  }
            //  catch (Exception e)
            //  {
            //    System.Diagnostics.Debug.WriteLine("_file upload error.", e);
            //  }
            //}

            //signAndEncrypt(request);
            //request.setAbsoluteURL(serverUrl);
            //request.encoding();

            //foreach (string key in original.AllKeys)
            //{
            //  //alternate.put(key, new ArrayList<Object>(original.get(key)));
            //  alternate.Add(key, original.Get(key));
            //}

            ////string content = getRestTemplate(request).postForObject(serverUrl, alternate, String.class);
            ////  //if (logger.isDebugEnabled()) {
            ////  //    logger.debug("response:\n" + content);
            ////  //}
            ////  return content;
            //return null;
        }