【问题标题】:C# post method works on localhost but not on shared hostC# post 方法适用于本地主机,但不适用于共享主机
【发布时间】:2019-07-29 11:17:44
【问题描述】:

我正在测试一个 C# 应用程序,它使用 post 方法从“myhost/post_test”获取数据:

var client = new WebClient();
var data = new NameValueCollection();
data["email"] = email_input.Text;
data["password"] = pw_input.Text;

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol 
= SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;

var response = client.UploadValues("myhost/post_test", "POST", data);

对于 myhost 是 http://localhost:8000/ 它可以工作,但是

对于 myhost 是 https://example.com/ 它不是(返回 405 错误代码 - 不允许使用方法)。

我的网站使用的是 Laravel 5.7 框架,.htaccess 文件如下:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Authorization Header
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “alt-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-alt-php72___lsphp .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

是什么导致了这个问题?提前致谢。

编辑 1:我将把我使用的路线放在这里:

Route::group(['prefix'=>'client_server_protocol'],function(){
Route::group(['prefix'=>'version19'],function(){
    Route::post('login','AppController@postLogin');
    Route::post('AuthCheck','AppController@postAuthCheck');
    Route::post('ask_hrs_to_auth_check','AppController@postAsk_hrs_to_auth_check');

});
});

在“登录”Route::post 失败

【问题讨论】:

  • Laravel 中的 405 错误表示路由不存在。确保你已经在 Laravel 中设置了路由。
  • 其他路线都运行良好,不可能只有这条路线吧?
  • 405 通常意味着路由是使用与您发出请求不同的方法定义的。您可以发布您的路线文件吗?
  • 我的帖子更新了,请看一下

标签: c# laravel cpanel shared-hosting


【解决方案1】:

我已经想通了,我将这些添加到 webclient 对象并解决了问题:

       client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
       client.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
       client.Headers["Accept-Language"] = "en-us,en;q=0.5";
       client.Headers["Accept-Encoding"] = "gzip,deflate";
       client.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";

【讨论】:

    猜你喜欢
    • 2016-01-28
    • 1970-01-01
    • 2016-01-14
    • 2012-07-31
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多