pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

修復 - System.Net.WebException:遠程服務器返回錯誤:

Fixing - System.Net.WebException: The remote server returned an error: (500) Syntax error, command unrecognized(修復 - System.Net.WebException:遠程服務器返回錯誤:(500)語法錯誤,命令無法識別) - IT屋-程序員軟件開發技
本文介紹了修復 - System.Net.WebException:遠程服務器返回錯誤:(500)語法錯誤,命令無法識別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我創建了 FTP 代碼來傳輸文件.此代碼工作正常,但有時會導致錯誤 500.確切的錯誤是 -

I created FTP code to transfer files. This code works fine except that it sometimes causes an error 500. The exact error is -

Error: System.Reflection.TargetInvocationException: Exception has 
been thrown by the target of an invocation. 
---> System.Net.WebException: The remote server returned an error: 
(500) Syntax error, command unrecognized.
   at System.Net.FtpWebRequest.CheckError()
   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
   at System.Net.FtpWebRequest.GetRequestStream()
   at ST_772dn22cj49ndfddatee.csproj.ScriptMain.Main()
   --- End of inner exception stack trace --- 

我注意到在加載最大文件(即大約 290 KB)時會發生錯誤.所有其他文件都少于此,我對它們也不例外.我不知道為什么會這樣.誰能告訴我為什么?

I noticed that the error occurs when the biggest file is loaded, ie about 290 KB. All other files are less than this and i get no exception for them. I don't know why this happens. Can someone tell me why ?

順便說一句,如果您發現我的代碼有一些改進空間或邏輯錯誤,請同時提及.我并不是真的在尋找代碼審查,但它是受歡迎的.

As an aside, in case you notice some room for improvement in my code or logical error, then please mention that as well. I am not really looking for code reviews, but its welcome.

public void Main()
{

    Boolean conditions = true;

    if(conditions == true)
    {
    string fileLocation = "my windows directory";
    string fileName = "fileName.extension";

    string ftpFolder = @"/ftpFolder/";
    Boolean ftpMode = true; //passive or active. True = passive 
    string ftpPassword = "password";
    int ftpPort = 21;// the default
    string ftpServerName = "server name";
    string ftpUserName = "user name";

    //Create an object to communicate with the server.
    string ftpRequestString = "ftp://" + ftpServerName + ":" 
    + ftpPort + ftpFolder + fileName; 

    try{

    FtpWebRequest request = 
    (FtpWebRequest)WebRequest.Create(ftpRequestString);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

    //Set mode
    if(ftpMode == true){
        request.UsePassive = true;
    }

    //Copy the file to the request.

    string filePath = @fileLocation + "\" + fileName;
    StreamReader sourceStream = new StreamReader(filePath);
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    response.Close();

    }
     catch (WebException ex)
     {
        MessageBox.Show(ex.Message);

     }//try-catch

    }

}//main

推薦答案

在閱讀您的問題時,我懷疑這與將 KeepAlive 設置為 有關(或可以通過以下方式糾正)錯誤.看著 SO - 這個問題引用了同樣的問題并指出它:https://stackoverflow.com/a/2071374/1803682

On reading your question I was suspicious this has to do with (or could be corrected by) setting the KeepAlive to false. Looking on SO - this question references the same problem and points to it as well: https://stackoverflow.com/a/2071374/1803682

嘗試設置:

request.KeepAlive = false;

KeepAlive 設置為 false 您的連接將是 在每個請求結束時關閉.如果您要傳輸大量文件,這可能是一個問題 - 因為重新發送憑據等需要時間.好處是您以已知/初始狀態重新創建連接,這應該可以解決您的問題(即使它不是根原因).

With KeepAlive set to false your connection will be closed at the end of each request. If you are transmitting a lot of files this could be an issue - as it takes time to resend credentials, etc. The upside is you recreate the connection in a known / initial state which should solve your problem (even if it is not the root cause).

要查看發生了什么,如果您可以在服務器上啟用詳細日志記錄,您應該會在看到返回此錯誤之前看到發出的最后一條命令.這應該讓您更好地了解發生了什么.發現這個帖子說了很多同樣的話.

To see what is going on, if you can enable detailed logging on your server you should see the last command issued before seeing this error returned. This should give you a better idea of what is up. Found this thread saying much the same thing.

更新:

如果我閱讀了我自己發布的鏈接的底部,我可能會回答得更好,重新發出的命令可能是登錄過程的一部分(即 USER 用戶名),這是您可能遇到的問題:

If I had read to the bottom of the link I posted myself I could have answered even better, the command probably being reissued is some part of the login process (i.e. USER username) and this is your likely issue:

憑證可能不再有效的原因是WebRequest 使用在一定時間后到期的租約.如果您沒有明確實例化租約并定義其超時,FtpWebRequest 似乎使用默認超時值.我相信發生的事情是,當租約到期時,FtpWebRequest 將然后嘗試重新登錄.

The reason the creadentials may no longer be valid is that the WebRequest uses a lease that expires after a certain amount of time. If you don't explicitly instantiate the lease and define its timeouts, the FtpWebRequest appears to use default timeout values. I believe what's happening is that when the lease expires the FtpWebRequest will then try to log on again.

看起來 這里 使用正確的搜索:

So looking here with the right search:

導致等待請求的默認超時不是無限的,因為 指定,但實際上10000 ms.這似乎是一個很大的差異.所以你也可以嘗試設置:

yields that the default timeout waiting for requests is not infinite as specified but actually 10000 ms. Which seems a pretty big discrepancy. So you can also try setting:

request.Timeout = -1;

看看它是否能糾正你的錯誤.

And see if it corrects your error.

真的不認為這可能是您的問題,所以將其移至底部:

Really don't think this could be your issue so moving it to the bottom:

另外 - 檢查您的 request.ReadWriteTimeout 是否適合您看到的較大文件的速度.默認值為 5分鐘 這對于 290k 來說會很長,所以我希望這不是你的錯誤的根源.另外 - 如果這是問題,我預計會出現連接關閉錯誤.

Also - check that your request.ReadWriteTimeout is appropriate for the speed you see for the larger file. The default is 5 minutes which would be pretty long for 290k, so I expect this is not the source of your error. Also - I would expect a connection closed error if this was the problem.

這篇關于修復 - System.Net.WebException:遠程服務器返回錯誤:(500)語法錯誤,命令無法識別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 烟台金蝶财务软件,烟台网站建设,烟台网络推广 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 伸缩器_伸缩接头_传力接头-巩义市润达管道设备制造有限公司 | 超声波清洗机-超声波清洗设备定制生产厂家 - 深圳市冠博科技实业有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 无锡网站建设_小程序制作_网站设计公司_无锡网络公司_网站制作 | 驾驶人在线_专业学车门户网站| 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 广东西屋电气有限公司-广东西屋电气有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | 桐城新闻网—桐城市融媒体中心主办 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 济南办公室装修-厂房装修-商铺装修-工装公司-山东鲁工装饰设计 | 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 温室大棚建设|水肥一体化|物联网系统 | 升降炉_真空气氛炉_管式电阻炉厂家-山东中辰电炉有限公司 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 机械加工_绞车配件_立式离心机_减速机-洛阳三永机械厂 | 动物麻醉机-数显脑立体定位仪-北京易则佳科技有限公司 | 塑料熔指仪-塑料熔融指数仪-熔体流动速率试验机-广东宏拓仪器科技有限公司 |