【问题标题】:How to POST multiple xml to a WebService如何将多个 xml 发布到 WebService
【发布时间】:2011-10-20 08:20:49
【问题描述】:

实际上,我在 c# 中有代码来使用 POST 方法执行 httpwebrequest,发送一个 XML 并接收一个 XML 答案并保存它,这里是代码:

public static XmlDocument PostXMLTransaction(string v_strURL)
{
//Declare XMLResponse document
XmlDocument XMLResponse = null;

//Declare an HTTP-specific implementation of the WebRequest class.
HttpWebRequest objHttpWebRequest;

//Declare an HTTP-specific implementation of the WebResponse class
HttpWebResponse objHttpWebResponse = null;

//Declare a generic view of a sequence of bytes
Stream objRequestStream = null;
Stream objResponseStream = null;

//Declare XMLReader
XmlTextReader objXMLReader;

//Creates an HttpWebRequest for the specified URL.
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);

try
{
//---------- Start HttpRequest 

//Set HttpWebRequest properties
byte[] bytes;
bytes = System.Text.Encoding.UTF8.GetBytes("C:\\Documents\ \Aplicacion1\\Aplicacion1\\absisSIHttpProfileTO.xm l");
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentLength = bytes.Length;
objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

//Get Stream object 
objRequestStream = objHttpWebRequest.GetRequestStream();

//Writes a sequence of bytes to the current stream 
objRequestStream.Write(bytes, 0, bytes.Length);

//Close stream
objRequestStream.Close();

//---------- End HttpRequest

//Sends the HttpWebRequest, and waits for a response.
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

//---------- Start HttpResponse
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
//Get response stream 
objResponseStream = objHttpWebResponse.GetResponseStream();

//Load response stream into XMLReader
objXMLReader = new XmlTextReader(objResponseStream);

//Declare XMLDocument
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(objXMLReader);
xmldoc.Save(@"C:\Documents\Aplicacion1\Aplicacion1\myxml.xml");

//Set XMLResponse object returned from XMLReader
XMLResponse = xmldoc;

//Close XMLReader
objXMLReader.Close();
}

//Close HttpWebResponse
objHttpWebResponse.Close();
}
catch (WebException we)
{
//TODO: Add custom exception handling
throw new Exception(we.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
//Close connections
objRequestStream.Close();
objResponseStream.Close();
objHttpWebResponse.Close();

//Release objects
objXMLReader = null;
objRequestStream = null;
objResponseStream = null;
objHttpWebResponse = null;
objHttpWebRequest = null;
}

//Return
return XMLResponse;
}

我现在的问题是如何在一个 httpwebrequest 中发送多个 XML 作为参数?

喜欢: 公共静态 XmlDocument PostXMLTransaction(xmldocument xml1, xmldocument xml2)

【问题讨论】:

  • 顺便说一句,您没有在代码中发送 xml,您只是发送字符串 C:\\Documents\ \Aplicacion1\\Aplicacion1\\absisSIHttpProfileTO.xm l
  • 这段代码:objHttpWebRequest.Method = "POST"; objHttpWebRequest.ContentLength = bytes.Length; objHttpWebRequest.ContentType = "文本/xml;编码='utf-8'";不是用来发送 XML 的吗?
  • bytes 包含c:\\documents..... 字符串,而不是文件内容
  • 如何发送带有 xml 作为参数的 httpwebrequest?
  • 将文件内容读入bytes(StreamReader、File、FileStream等)

标签: c# wpf xml post httpwebrequest


【解决方案1】:

嗯,这确实取决于您的协议。另一端的服务器需要知道如何通过 1 个连接处理多条消息。如果服务器是一个 http 网络服务器,那么你会很幸运,因为该协议已经定义了一种重用连接的机制。 (尽管客户端和服务器都必须同意使用它)。

为此,您可以使用 HttpWebRequest 的 KeepAlive 属性,请参阅:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2019-11-07
    • 2016-11-10
    相关资源
    最近更新 更多