如何将多个 xml POST 到 WebService
实际上,我有 C# 代码来执行 httpwebrequest,并使用 POST 方法发送一个 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)
Actually I have the code in c# to do an httpwebrequest with a POST method sending one XML and receiving an XML answer and saving it, here is the code:
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;
}
Mi question now is how can I send multiple XML as parameters in one httpwebrequest?
Like:
public static XmlDocument PostXMLTransaction(xmldocument xml1, xmldocument xml2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这确实取决于您的协议。另一端的服务器需要知道如何通过一个连接处理多个消息。如果服务器是 http Web 服务器,那么您很幸运,因为该协议已经定义了重用连接的机制。 (尽管客户端和服务器都必须同意使用它)。
为此,您可以使用 HttpWebRequest 的 KeepAlive 属性,请参阅:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx
Well, that relies on your protocol really. The server on the other side needs to know how to handle multiple messages over 1 connection. If the server is a http webserver than your lucky since that protocol already defines a mechanism of reusing a connection. (Although both the client and the server have to agree on using this).
For this, you can play with the KeepAlive property of HttpWebRequest, see: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx