.net giving 400 bad request on get request made from certain server

左心房为你撑大大i 提交于 2019-12-24 16:19:04

问题


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

回答1:


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 like 127.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"


来源:https://stackoverflow.com/questions/33624749/net-giving-400-bad-request-on-get-request-made-from-certain-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!