C#构建一个Web服务方法,它接受像HttpWebRequest方法这样的POST方法

时间:2013-02-13 13:25:54

标签: c# post service web

我需要一个接受POST方法的Web服务。正在访问我的服务器正在使用POST方法。 它发送给我一个xml,我应该回复一些xml。

另一方面,当我访问他时,我已经使用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方法的web服务。 有谁知道如何做到这一点。我被困在这里

2 个答案:

答案 0 :(得分: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";
    }

[如果struct将返回类型更改为相应的struct类型]

以下是一个函数,您可以在其中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;
    }

电话会是:

 XmlDocument XMLdoc = new XmlDocument();
 XMLdoc.Load("<xml file locatopn>");
 XmlDocument response = PostXMLTransaction("<The WebService URL>", XMLdoc);
 string source = response.OuterXml;

[如果这有用或需要更多帮助请告诉我们]

答案 1 :(得分:0)

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

  

可以通过编辑Web.config文件来启用HTTP GET和HTTP POST   对于Web服务所在的vroot。下列   配置启用HTTP GET和HTTP POST:

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