>我还有其他一些帖子,我一直在将数据发布到 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)
如果我在浏览器中点击网址,我会得到HTTP 405 Method Not Allowed
想知道服务器是否由于数据错误而忽略了帖子,或者我缺少标题或类似的东西。
我已经浏览了打印机基本服务的 upnp.org 文档,以及 SOAP 1.1 UPnP 配置文件,这两者都对我有很大帮助。
有人有什么想法吗?
这是我的 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>
阻止我打印的一个问题是打印机设置为询问从哪个托盘打印。这会阻止打印邮件。这是最后一条 SOAP 消息。我没有逃脱它。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>test</title></head>
<body><div>hello world</div></body>
</html>
</s:Body>
</s:Envelope>