2013-02-13 68 views
0

我需要一個接受POST方法的Web服務。正在訪問我的服務器正在使用POST方法。 它發送給我一個XML,我應該回應一些XML。C#構建一個接受POST方法(如HttpWebRequest方法)的websevice方法

另一種方式,當我訪問他時,我已經與HttpWebRequest類進行管理,它工作正常。它是這樣做的:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s.strMvrataUrl.ToString()); 
req.ClientCertificates.Add(cert); 
req.Method = "POST"; 
req.ContentType = "text/xml; encoding='utf-8'"; 
s.AddToLog(Level.Info, "Certifikat dodan."); 
byte[] bdata = null; 
bdata = Encoding.UTF8.GetBytes(strRequest); 
req.ContentLength = bdata.Length; 
Stream stremOut = req.GetRequestStream(); 
stremOut.Write(bdata, 0, bdata.Length); 
stremOut.Close(); 
s.AddToLog(Level.Info, "Request: " + Environment.NewLine + strRequest); 
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream()); 
strResponse = streamIn.ReadToEnd(); 
streamIn.Close(); 

現在我想有一個接受POST方法的webservice。 有沒有人有一個想法如何做到這一點。我被困在這裏。

+0

你問如何發佈到Web服務或如何建立一個Web服務接受一個職位? – 2013-02-13 13:43:58

+0

我認爲兩者都會有所幫助。請幫助 – 2013-02-13 13:49:07

+0

如果可能的話,請考慮切換到asp.net web-api。 – adt 2013-02-13 14:09:29

回答

1

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; 

[如果這是有益的或需要更多的幫助,請讓我知道]

+0

你能給我一個WebMethod的例子嗎,她應該怎麼樣?因爲我設法打電話POST這個WebMethod:[WebMethod] 公共字符串HelloWorld(字符串str,字符串str1) – 2013-02-13 14:20:02

+0

Tnak你爲你的答案。這是一個客戶端功能,我對此沒有任何問題。我的WebService [webmethod]有問題,我的webservice方法應該如何處理這種POST調用? – 2013-02-14 07:20:59

+0

@MarkoLeben:我在編輯I中給出了一個例子:[WebMethod] public string HelloWorld(){ return「HelloWorld」; } – 2013-02-14 07:33:33

0

http://support.microsoft.com/kb/819267

HTTP GET和HTTP POST可以通過編輯Web.config文件 用於在Web服務所在的虛擬根被啓用。下面 配置使得HTTP GET和HTTP POST:

<configuration> 
    <system.web> 
    <webServices> 
     <protocols> 
      <add name="HttpGet"/> 
      <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
    </system.web> 
</configuration> 
+0

我已經這樣做了。 Belive我搜查了網絡。但我應該如何使用POST方法訪問它與一些XML? – 2013-02-13 13:31:29