本文介绍了WebClient请求期间发生异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果(strFileType =="FullFill")
                    strURLFullfill ="https://www.xxx.com/SF3KmDGwK/notify_shipment.php";
                            其他
                    strURLFullfill ="https://www.yyy.com/SF3KmDGwK/notify_receipt.php";


                             //检查输出文件是否存在,然后再上传
                             //while(!blnNotFound)
                             //for(int j = 0; j< arrstrFiles.Length; j ++)
                             //{
                    WebClient wc2 =新的WebClient();
                    Uri地址2 =新Uri(strURLFullfill);
                    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                    NetworkCredential nc2 = new NetworkCredential();
                    credentialCache.Add(new System.Uri(strURLFullfill),"Basic",nc2);
                    nc2.UserName ="xxx";
                    nc2.Password ="yyy";
                    authinfo2 = nc2.UserName +:" + nc2.Password;
                    authinfo2 = Convert.ToBase64String(Encoding.ASCII.GetBytes(authinfo2));
                    wc2.UseDefaultCredentials = false;
                    //wc2.Credentials = new NetworkCredential(nc2.UserName,nc2.Password);
                    wc2.Headers [HttpRequestHeader.Authorization] = string.Format("Basic {0}",authinfo2);
                    wc2.Headers ["User-Agent"] =
              nbsp; bsp    "Mozilla/4.0(兼容; Windows NT 5.1; MSIE 6.0)" +
              nbsp; bsp    (兼容; MSIE 6.0; Windows NT 5.1;"+".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                    wc2.BaseAddress = address2.ToString();

                    //如果(intSuccess == 200)
                    //    File.Delete(PATH +"Output \\" + flInfo.Name +".xml"));
                    //else
                    //    SendEmail("VitaminShield上载完整文件失败",上载" + flInfo.Name +.xml" 失败:错误代码"+ intSuccess.ToString());

                   试试
                    {
               b wc2.UploadFile(strURLFullfill,strFile);
               b responseField = wc2.GetType().GetField("m_WebResponse",BindingFlags.Instance | BindingFlags.NonPublic);
               b response = responseField.GetValue(wc2)as HttpWebResponse;

               b如果(response.StatusCode.ToString()!=确定")
              nbsp; bsp    cls.SendEmail(上传文件满载失败",文件的上传" + strURLFullfill +"失败:状态为"+ response.StatusCode + quot ;:"
              nbsp; bsp           + response.StatusDescription);

               b File.Delete(strURLFullfill);
                    }
                    catch(异常例外)
                    {
               b cls.SendEmail(上传/删除完全填充文件失败",上传/删除" + strURLFullfill +"失败:"+ ex.Message);
                    }
                   终于
                    {
               b wc2.Dispose();
                    }

                if (strFileType == "FullFill")
                    strURLFullfill = "https://www.xxx.com/SF3KmDGwK/notify_shipment.php";
                else
                    strURLFullfill = "https://www.yyy.com/SF3KmDGwK/notify_receipt.php";


                //Check to see if output files exist before I upload them
                //while (!blnNotFound)
                //for (int j = 0; j < arrstrFiles.Length; j++)
                //{
                    WebClient wc2 = new WebClient();
                    Uri address2 = new Uri(strURLFullfill);
                    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                    NetworkCredential nc2 = new NetworkCredential();
                    credentialCache.Add(new System.Uri(strURLFullfill), "Basic", nc2);
                    nc2.UserName = "xxx";
                    nc2.Password = "yyy";
                    authinfo2 = nc2.UserName + ":" + nc2.Password;
                    authinfo2 = Convert.ToBase64String(Encoding.ASCII.GetBytes(authinfo2));
                    wc2.UseDefaultCredentials = false;
                    //wc2.Credentials = new NetworkCredential(nc2.UserName, nc2.Password);
                    wc2.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", authinfo2);
                    wc2.Headers["User-Agent"] =
                            "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
                            "(compatible; MSIE 6.0; Windows NT 5.1; " + ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                    wc2.BaseAddress = address2.ToString();

                    //if (intSuccess == 200)
                    //    File.Delete(PATH + "Output\\" + flInfo.Name + ".xml");
                    //else
                    //    SendEmail("VitaminShield Upload Fullfill file failure", "Uploading of " + flInfo.Name + ".xml failed: Error Code " + intSuccess.ToString());

                    try
                    {
                        wc2.UploadFile(strURLFullfill, strFile);
                        responseField = wc2.GetType().GetField("m_WebResponse", BindingFlags.Instance | BindingFlags.NonPublic);
                        response = responseField.GetValue(wc2) as HttpWebResponse;

                        if (response.StatusCode.ToString() != "OK")
                            cls.SendEmail("Upload Fullfill file failure", "Uploading of " + strURLFullfill + " failed: Status " + response.StatusCode + ": "
                                    + response.StatusDescription);

                        File.Delete(strURLFullfill);
                    }
                    catch (Exception ex)
                    {
                        cls.SendEmail("Upload/Delete Fullfill file failure", "Uploading/Deleting of " + strURLFullfill + " failed: " + ex.Message);
                    }
                    finally
                    {
                        wc2.Dispose();
                    }

这是对以下陈述的夸大:                 b wc2.UploadFile(strURLFullfill,strFile);

This is blowing up on the following statement:                          wc2.UploadFile(strURLFullfill, strFile);

请让我知道为什么.

推荐答案

请让我知道原因.

您可以麻烦发布错误消息吗?

Can you bother posting the error message?


这篇关于WebClient请求期间发生异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:30