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

修復(fù) - System.Net.WebException:遠(yuǎn)程服務(wù)器返回錯誤:

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

問題描述

限時送ChatGPT賬號..

我創(chuàng)建了 FTP 代碼來傳輸文件.此代碼工作正常,但有時會導(dǎo)致錯誤 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)時會發(fā)生錯誤.所有其他文件都少于此,我對它們也不例外.我不知道為什么會這樣.誰能告訴我為什么?

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 ?

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

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 設(shè)置為 有關(guān)(或可以通過以下方式糾正)錯誤.看著 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

嘗試設(shè)置:

request.KeepAlive = false;

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

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).

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

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.

更新:

如果我閱讀了我自己發(fā)布的鏈接的底部,我可能會回答得更好,重新發(fā)出的命令可能是登錄過程的一部分(即 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 使用在一定時間后到期的租約.如果您沒有明確實例化租約并定義其超時,F(xiàn)tpWebRequest 似乎使用默認(rèn)超時值.我相信發(fā)生的事情是,當(dāng)租約到期時,F(xiàn)tpWebRequest 將然后嘗試重新登錄.

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:

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

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.

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

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

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

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.

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

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

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進(jìn)行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
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(如何獲取守護(hù)進(jìn)程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
.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(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 低压载波电能表-单相导轨式电能表-华邦电力科技股份有限公司-智能物联网综合管理平台 | 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 佛山商标注册_商标注册代理|专利注册申请_商标注册公司_鸿邦知识产权 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 翅片管换热器「型号全」_厂家-淄博鑫科环保 | WF2户外三防照明配电箱-BXD8050防爆防腐配电箱-浙江沃川防爆电气有限公司 | 安全,主动,被动,柔性,山体滑坡,sns,钢丝绳,边坡,防护网,护栏网,围栏,栏杆,栅栏,厂家 - 护栏网防护网生产厂家 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 脉冲布袋除尘器_除尘布袋-泊头市净化除尘设备生产厂家 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 生物除臭剂-除味剂-植物-污水除臭剂厂家-携葵环保有限公司 | 撕碎机_轮胎破碎机_粉碎机_回收生产线厂家_东莞华达机械有限公司 | 巩义市科瑞仪器有限公司| 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | 中药超微粉碎机(中药细胞级微粉碎)-百科| 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 包头市鑫枫装饰有限公司| 无机纤维喷涂棉-喷涂棉施工工程-山东华泉建筑工程有限公司▲ | 苏州柯瑞德货架-仓库自动化改造解决方案 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 上海阳光泵业制造有限公司 -【官方网站】 | 集菌仪_智能集菌仪_全封闭集菌仪_无菌检查集菌仪厂家-那艾 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 代做标书-代写标书-专业标书文件编辑-「深圳卓越创兴公司」 | 板式换网器_柱式换网器_自动换网器-郑州海科熔体泵有限公司 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 无锡网站建设-做网站-建网站-网页设计制作-阿凡达建站公司 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 国产液相色谱仪-超高效液相色谱仪厂家-上海伍丰科学仪器有限公司 | 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 高压微雾加湿器_工业加湿器_温室喷雾-昌润空气净化设备 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 蓄电池回收,ups电池后备电源回收,铅酸蓄电池回收,机房电源回收-广州益夫铅酸电池回收公司 | 广州食堂承包_广州团餐配送_广州堂食餐饮服务公司 - 旺记餐饮 | 企业微信scrm管理系统_客户关系管理平台_私域流量运营工具_CRM、ERP、OA软件-腾辉网络 |