可以在配置中启用 HTTP GET 和 HTTP POST。根目录中有一个名为 webconfig 的文件,您必须在其中添加以下设置:
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
即在system.web 标签内。
现在在 web 服务中,如果您打算发回 XML,您可以设计一个类似于预期 XML 的结构。例如:获取以下类型的 XML:
<?xml version="1.0" encoding="utf-8"?>
<Quote xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<object>Item101</object>
<price>200</price>
</Quote>
您必须从 Web 服务返回以下结构的对象:
public struct Quote
{
public int price;
public string object;
public Quote(int pr, string obj)
{
price = pr;
object = obj
}
}
现在可以将其作为字符串作为响应接收,然后根据需要进行解析。
==================================================== ==============================
编辑我
下面是一个HelloWorld WebMethod
[WebMethod]
public string HelloWorld() {
return "HelloWorld";
}
[在结构的情况下,将返回类型更改为相应的结构类型]
以下是一个功能,您可以 POST 到 URL,也可以将 xml 文件发送到 web 服务[根据您的需要使用]:
public static XmlDocument PostXMLTransaction(string URL, XmlDocument XMLDoc)
{
//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(URL);
try
{
//---------- Start HttpRequest
//Set HttpWebRequest properties
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(XMLDoc.InnerXml);
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);
//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;
}
And the call would be like :
XmlDocument XMLdoc = new XmlDocument();
XMLdoc.Load("<xml file locatopn>");
XmlDocument response = PostXMLTransaction("<The WebService URL>", XMLdoc);
string source = response.OuterXml;
[If this was helpful or need more help please let me know]