.NET提供的get请求400错误的请求来自特定服务器进行 [英] .net giving 400 bad request on get request made from certain server
问题描述
如果我在我的电脑上运行IE浏览器作为用户 DOMAIN \\自动
的网址如下:
右键点击IE浏览器
Shift +右键单击Internet Explorer的菜单
为运行不同的用户
当打开链接的http://服务器/ ClearCache 没有问题
当我做同样的使用PowerShell和 DOMAIN再次运行\\自动
下面的脚本运行:
$ URL =HTTP://服务器/ ClearCache
$ REQ = [system.Net.WebRequest] ::创建($网址)
$ req.UseDefaultCredentials = $真
$解析度= $ req.GetResponse()
$水库
路由ClearCache有 [使用AllowAnonymous]
在控制器中。
当我去与远程桌面服务器登录的用户 DOMAIN \\自动
需要运行PowerShell脚本(SQL Server代理作业步骤),并运行该脚本在PowerShell中,我得到:
异常调用的GetResponse和0的说法(S):远程
服务器返回错误:(400)错误的请求在行:1字符:1
+ $解析度= $ req.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:引发WebException
块引用>打开网址在IE浏览器给我的答复400错误的请求。
这似乎是因为Chrome的服务器上可以打开链接直接涉及到一个IE浏览器配置文件在服务器上。
请问有谁知道什么章第键或在我需要改变,以使这项工作方案的海洋设置?
感谢您阅读我的问题。
[更新]
当试图运行它在我的凭据我仍然得到400错误,所以不要认为这是因为身份验证:
$ URL =HTTP://服务器/ ClearCache
$ REQ = [system.Net.WebRequest] ::创建($网址)
#$ req.UseDefaultCredentials = $真
$ passwd文件=的ConvertTo-SecureString的密码-AsPlainText -Force;
$ req.Credentials =新对象System.Management.Automation.PSCredential(我的帐户,$ passwd文件);
$解析度= $ req.GetResponse()
$水库
解决方案不知道问题是什么,或者为什么它曾在Chrome,但不是IE浏览器。由于响应体(可以看到IE devtools响应主体时,preSS F12)说:无效的主机名我试着补充一些DNS绑定在
C:\\ WINDOWS \\ SYSTEM32 \\ DRIVERS \\等\\主机
如127.0.0.1 MYSERVER
键,取消注释本地主机,但没有结果。最后的IP地址替换服务器名称和解决它(XXX是IP地址数):
$ URL =http://xxx.xxx.xxx.xxx/ClearCache
If I run IE on my computer as the user
DOMAIN\automatic
the url works:right click IE shift + right click "Internet Explorer" in the menu run as different user
When opening the url http://server/ClearCache there is no problem.
When I do the same with powershell and run it again as
DOMAIN\automatic
the following script runs:$url = "http://server/ClearCache" $req = [system.Net.WebRequest]::Create($url) $req.UseDefaultCredentials = $true $res = $req.GetResponse() $res
The route ClearCache has
[AllowAnonymous]
in the controller.When I go to the server with remote desktop logged in as user
DOMAIN\automatic
that needs to run the powershell script (sql server agent job step) and run the script in powershell I get:Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request." At line:1 char:1 + $res = $req.GetResponse() + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
Opening the url in IE gives me the response 400 Bad Request.
This seems to be related to an IE profile on the server because Chrome on the server can open the url.
Would anyone know what reg key or setting in the ocean of options I need to change to make this work?
Thank you for reading my question.
[UPDATE]
When trying to run it under my credentials I still get the 400 error so don't think it is because of authentication:
$url = "http://server/ClearCache" $req = [system.Net.WebRequest]::Create($url) # $req.UseDefaultCredentials = $true $passwd = ConvertTo-SecureString "password" -AsPlainText -Force; $req.Credentials = New-Object System.Management.Automation.PSCredential ("myAccount", $passwd); $res = $req.GetResponse() $res
解决方案Not sure what the problem was or why it worked in Chrome but not IE. Since the response body (can see the response body in IE devtools when you press F12) says "Invalid host name" I tried to add some DNS bindings in
C:\Windows\System32\drivers\etc\hosts
like127.0.0.1 myserver
and uncommenting localhost but with no result.Finally replaced the server name with the ip address and that solved it (xxx are numbers of ip address):
$url = "http://xxx.xxx.xxx.xxx/ClearCache"
这篇关于.NET提供的get请求400错误的请求来自特定服务器进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!