示例#1
0
        public HttpParamers GetHttpParamers()
        {
            HttpParamers paramers = HttpParamers.GetParamers();

            paramers.AddParamer("contractId", this.contractId);
            return(paramers);
        }
示例#2
0
 public HttpResponseMessage Get(string requestData)
 {
     try
     {
         if (string.IsNullOrEmpty(requestData))
         {
             return(Request.CreateResponse(System.Net.HttpStatusCode.OK));
         }
         HttpParamers paramers = HttpParamers.GetParamers();
         paramers.AddParamer("requestData", requestData);
         Stream stream = new MemoryStream();
         Http.HttpClient.DoDownload(config.AccessUrl, paramers, GetHttpHeader(), QiyuesuoConfig.CONNECT_TIMEOUT, QiyuesuoConfig.READ_TIMEOUT, ref stream);
         return(new HttpResponseMessage {
             Content = new ByteArrayContent(((MemoryStream)stream).ToArray())
         });
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(new HttpResponseMessage {
             StatusCode = System.Net.HttpStatusCode.InternalServerError, Content = new StringContent(e.Message)
         });
     }
 }
示例#3
0
        public async System.Threading.Tasks.Task <HttpResponseMessage> Post()
        {
            Stream fileStream = new MemoryStream();

            try
            {
                string requestData = null;
                Dictionary <string, string> dic = new Dictionary <string, string>();
                // multipart/form-data 格式请求 解析文件流和参数内容
                if (Request.Content.IsMimeMultipartContent())
                {
                    var multipartMemoryStreamProvider = await Request.Content.ReadAsMultipartAsync();

                    foreach (var content in multipartMemoryStreamProvider.Contents)
                    {
                        // 获取 file 对应的文件流 保存到 fileStream
                        if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName) && content.Headers.ContentDisposition.Name.Equals("\"file\""))
                        {
                            using (Stream stream = await content.ReadAsStreamAsync())
                            {
                                byte[] bytes = new byte[stream.Length];
                                stream.Read(bytes, 0, bytes.Length);
                                fileStream          = new MemoryStream(bytes);
                                fileStream.Position = 0;
                            }
                        }
                        else
                        {
                            string val = await content.ReadAsStringAsync();

                            dic.Add(content.Headers.ContentDisposition.Name.Replace("\"", ""), System.Web.HttpUtility.UrlDecode(val));
                        }
                    }
                    // 获取 requestData值
                    dic.TryGetValue("requestData", out requestData);
                }
                // 非mulitpart/form-data 格式请求:application/x-www-form-urlencoded
                else
                {
                    // 获取 requestData值
                    string provider = await Request.Content.ReadAsStringAsync();

                    requestData = System.Web.HttpUtility.UrlDecode(provider.Substring(12));
                }
                // 组装请求请求到远程服务器
                HttpParamers paramers = HttpParamers.GetParamers();
                paramers.AddParamer("requestData", requestData);
                if (fileStream != null && fileStream.Length != 0)
                {
                    paramers.AddFile("file", new StreamFile(fileStream));
                }
                String response = Http.HttpClient.DoPost(config.AccessUrl, paramers, GetHttpHeader(), QiyuesuoConfig.CONNECT_TIMEOUT, QiyuesuoConfig.READ_TIMEOUT);
                return(new HttpResponseMessage {
                    Content = new StringContent(response)
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new HttpResponseMessage {
                    StatusCode = System.Net.HttpStatusCode.InternalServerError, Content = new StringContent(e.Message)
                });
            }
            finally
            {
                fileStream.Close();
            }
        }