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文件的WebService:
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;
}
和呼叫會像:
XmlDocument XMLdoc = new XmlDocument();
XMLdoc.Load("<xml file locatopn>");
XmlDocument response = PostXMLTransaction("<The WebService URL>", XMLdoc);
string source = response.OuterXml;
[如果這是有益的或需要更多的幫助,請讓我知道]
你問如何發佈到Web服務或如何建立一個Web服務接受一個職位? – 2013-02-13 13:43:58
我認爲兩者都會有所幫助。請幫助 – 2013-02-13 13:49:07
如果可能的話,請考慮切換到asp.net web-api。 – adt 2013-02-13 14:09:29