我还有其他一些帖子,我一直在将数据发布到 UPnP 打印机。基本上从头开始创建一个控制点(这仍然是 POC,我将在多种设备类型上实现,所以我试图首先获得基本的理解)。到目前为止,我能够发现打印机、请求打印作业并取回数据接收器 url 以发布要打印的内容。此时我尝试使用简单的 xhtml 数据进行 POST,但请求似乎每次都超时,但出现以下异常:
The remote server returned an error: NotFound.
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
如果我在浏览器中点击 url,我会回来HTTP 405 Method Not Allowed
我想知道服务器是否由于数据错误而忽略了帖子,或者我缺少标题或类似的东西。
我已经浏览了 upnp.org 上有关 Printer Basic 服务的文档,以及 SOAP 1.1 UPnP Profile,这两者都极大地帮助了我。
有人有什么想法吗?
这是我的 xhtml 的副本:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML-Print 1.0//EN\" \"http://www.w3.org/MarkUp/DTD/xhtml-print10.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>test</title>
</head>
<body>
<div>hello world</div>
</body>
</html>
我正在使用 C# 创建和发送HttpWebRequest
. 这是执行此操作的代码(我有几个变体,另一个使用WebClient
)
private void SendToPrinter(string printUri, string content)
{
var request = WebRequest.Create(new Uri(printUri)) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/xml; charset=\"utf-8\"";
request.ContentLength = content.Length;
request.Headers[SoapHeaderName] = CreateJobHeader; // this probably isn't necessary
request.BeginGetRequestStream(ar =>
{
var requestStream = request.EndGetRequestStream(ar);
using (var sw = new StreamWriter(requestStream))
{
sw.Write(content);
sw.Close();
}
request.BeginGetResponse(a =>
{
var response = request.EndGetResponse(a);
var responseStream = response.GetResponseStream();
using (var sr = new StreamReader(responseStream))
{
var results = sr.ReadToEnd();
}
}, null);
}, null);
}
来自提琴手的原始帖子数据
POST http://10.20.201.90/upnp/print/1d153438-1e90-1f0b-8b54-984be15df0fe HTTP/1.1
Content-Type: text/xml; charset="utf-8"
SOAPACTION: "urn:schemas-upnp-org:device:Printer:1#SendDocument"
Host: 10.20.201.90
Content-Length: 482
Expect: 100-continue
<?xml version="1.0" encoding="UTF-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:SendDocument xmlns:u="urn:schemas-upnp-org:device:Printer:1"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>test</title></head><body><div>hello world</div></body></html></u:SendDocument></s:Body></s:Envelope>