C#通用HttpPostDownLoad

C#通用HttpPostDownLoad

public static string PostHttpDownLoad(string Url, string JsonStr, string ContentType, string sFileName)
        {

            string dt = DateTime.Now.ToString("yyyy-MM-dd");
            string sDirectory = Application.ExecutablePath.Substring(0, 2) + @"\JQLog\";
            if (!Directory.Exists(sDirectory))
            {
                Directory.CreateDirectory(sDirectory);
            }

            StreamWriter sw = File.AppendText(sDirectory + dt + ".log");
            sw.WriteLine(DateTime.Now.ToString() + "\t" + "PostULR:" + Url + "\t" + "下载文件路径:" + sFileName + "\r\n" +
                "Post入参:" + JsonStr + "\r\n");
            sw.Flush();
            sw.Close();

            string strHtml = "";

            if (ContentType == "")
            {
                ContentType = "application/json";
            }

            #region 常规POST调用
            try
            {
                byte[] byteArray = null;

                byteArray = Encoding.UTF8.GetBytes(JsonStr.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t")); //转化

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(Url));

                request.Timeout = 60000;

                request.Method = "POST";
                request.ContentType = ContentType;
                request.ContentLength = byteArray.Length;
                Stream myRequestStream = request.GetRequestStream();
                myRequestStream.Write(byteArray, 0, byteArray.Length);//写入参数
                myRequestStream.Close();

                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)request.GetResponse();
                long totalBytes = myrp.ContentLength;

                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(sFileName, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();

                return "";

            }
            catch (WebException ex)//异常消息返回
            {
                HttpWebResponse res = ex.Response as HttpWebResponse;
                if (res != null) //if (res != null && (res.StatusCode == HttpStatusCode.InternalServerError))               
                {
                    Stream s = res.GetResponseStream();
                    StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("utf-8"));
                    string strLine = "";
                    //读取                      
                    while (strLine != null)
                    {
                        strLine = objReader.ReadLine();
                        if (strLine != null)
                        {
                            strHtml += strLine.Trim();
                        }
                    }
                }
                else
                {
                    strHtml = ex.Message;
                }

                sw = File.AppendText(sDirectory + dt + ".log");
                sw.WriteLine(DateTime.Now.ToString() + "调用异常:" + strHtml + "\r\n");
                sw.Flush();
                sw.Close();

                return strHtml;

            }
            #endregion

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

feisng0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值