gpt4 book ai didi

c# - 从 Compact Framework 客户端进行 REST 调用时如何避免 "Bad Request - Invalid Hostname"错误?

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:13 26 4
gpt4 key购买 nike

我使用了来自 here 的代码尝试从 Compact Framework 客户端调用 Web API 服务器应用程序上的 REST Controller 方法:

public static void SendXMLFile3(string uri, string data)
{
WebRequest request = WebRequest.Create (uri);
request.Method = "POST";
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
MessageBox.Show(((HttpWebResponse) response).StatusDescription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close ();
}

...我之前尝试过这段代码,它是从“Microsoft .NET Compact Framework”一书中获得的:

public static void SendXMLFile2(string uri, string data)
{
WebRequest req = WebRequest.Create(uri);
req.Method = "Post";
req.ContentType = "text/plain; charset=utf-8";
byte[] encodedBytes = Encoding.UTF8.GetBytes(data);
req.ContentLength = encodedBytes.Length;

Stream requestStream = req.GetRequestStream();
requestStream.Write(encodedBytes, 0, encodedBytes.Length);
requestStream.Close();

WebResponse result = req.GetResponse();
MessageBox.Show(result.ToString());
}

...但是我得到“400 - Bad Request”以及新的(以及旧的)代码。

我最初的尝试也没有成功,结果相同(400):

public static string SendXMLFile(string xmlFilepath, string uri, int timeout)
{
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(uri);
myHttpWebRequest.AllowWriteStreamBuffering=false;
string postData = "<Command><DSD><line_id>1</line_id><invoice_no>david_dsd</invoice_no>. . .</DSD></Command>"; // TODO: if this works, replace it with the real data
myHttpWebRequest.Method="POST";
UTF8Encoding encodedData = new UTF8Encoding();
byte[] byteArray=encodedData.GetBytes(postData);
myHttpWebRequest.ContentType = "application/xml";
myHttpWebRequest.ContentLength=byteArray.Length;
Stream newStream=myHttpWebRequest.GetRequestStream();
newStream.Write(byteArray,0,byteArray.Length);
newStream.Close();
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
return myHttpWebResponse.StatusDescription;
}

关于我尝试过的大量变体还有很多here ,我已经达到了我的帖子长度限制。

更新

请注意,服务器代码不知道/不关心该文件是 XML:

[HttpPost]
[Route("api/inventory/sendXML/{userId}/{pwd}/{filename}")]
public async Task SendInventoryXML(String userId, String pwd, String fileName)
{
Task task = Request.Content.ReadAsStreamAsync().ContinueWith(t =>
{
var stream = t.Result;
using (FileStream fileStream = File.Create(String.Format(@"C:\HDP\{0}.xml", fileName), (int)stream.Length))
{
byte[] bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}
});
}

更新 2

我试过 Charles 看它是否会获取本地 HTTP 流量,但它也对此充耳不闻(就像 Fiddler,无论如何都没有特殊的服务)。这是 Charles 在收到“400 - Bad Request”错误后的样子:

enter image description here

更新 3

我在某个地方找到了这个建议,让 Fiddler 显示本地 HTTP 流量:

Tools--> Fiddler Options. Choose Connections tab. Check the 'USe PAC Script' option.

...但它没有用 - 我在收到“400(错误请求)”消息时仍然没有看到 HTTP 流量。

更新 4

我现在也在 Fiddler 2 中看到“400(错误请求)”;为了得到它,我在 Postman 中输入以下任何内容(从 CE/CF/手持应用程序调用时在 Fiddler 中看不到这个):

http://SHANNON2:21608/api/inventory/sendXML/su/su/blablee // Hostname
http://SHANNON2.:21608/api/inventory/sendXML/su/su/blablee // Hostname with Fiddler-fooler appended "."
http://192.168.125.50:21608/api/inventory/sendXML/su/su/blablee // IP Address

(如果我将主机名或 IP 地址替换为“localhost”,Fiddler 不会捕获任何内容)

注意:对于 Postman 中的这些 URL,我选择了“Post”(而不是 GET 等),并附加了一个 XML 文件。

Fiddler 中的 Inspectors.Headers 显示:

POST /api/inventory/sendXML/su/su/blablee HTTP/1.1

虽然我认为这是一个小小的调试胜利,但我仍然不明白为什么会出现“400”错误。

Fiddler 告诉我,在 Inspectors.WebView Pane 中:

Bad Request - Invalid Hostname

--------------------------------------------------------------------------------

HTTP Error 400. The request hostname is invalid.

怎么可能呢?当我从 Postman 运行它时,我在服务器中遇到了断点 - 如果主机名无效,为什么会到达它?

更新 5

测试来自 Fiddler Composer 和 Postman 的调用,我可以到达服务器代码中的断点的唯一方法是使用“localhost”——将其替换为 PC 的 IP 地址 (192.168.125.50) 或主机名 (SHANNON2) 无法到达断点。虽然“有趣”,但从手持设备调用“localhost”显然不是一种选择。

更新 6

相关新问题here .

更新 7

问题的症结在于在命令提示符下添加:

netsh http add urlacl url=http://shannon2:80/ user=everyone

...或者这个:

netsh http add urlacl url=http://shannon2:8080/ user=everyone

参见更新 5 here了解更多详情

最佳答案

如果您在从 Postman 调用 API 时遇到Bad Request - Invalid Hostname 问题,请补充我的观察。像这样。

enter image description here

考虑在 Headers 中添加 Host,它会起作用。

enter image description here

在有人无法查看图像后更新:如果图像未加载,那么基本上您必须这样做:

基本上,您必须添加Content-LengthHost。 Postman 会在发送请求时自动计算。这些可以通过在 Postman 中取消隐藏 auto-generated headers 在 postman 中启用。或者您可以自己设置。

关于c# - 从 Compact Framework 客户端进行 REST 调用时如何避免 "Bad Request - Invalid Hostname"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25730345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com