【问题标题】:C# build a websevice method that accepts POST methods like HttpWebRequest methodC# 构建一个接受 POST 方法(如 HttpWebRequest 方法)的 Web 服务方法
【发布时间】:2013-01-29 01:30:26
【问题描述】:

我需要一个接受 POST 方法的网络服务。访问我的服务器正在使用 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 方法的网络服务。 有谁知道如何做到这一点。我被困在这里。

【问题讨论】:

  • 您是在问如何发布到网络服务或如何构建一个接受帖子的网络服务?
  • 我认为两者都会有所帮助。请帮忙
  • 如果可以的话,考虑切换到asp.net web-api 。
  • 我没有使用 asp.net 的经验,所以我宁愿使用 Web 服务。对于这种问题是用asp.net的吗?

标签: c# post service web


【解决方案1】:

可以在配置中启用 HTTP GET 和 HTTP POST。根目录中有一个名为 webconfig 的文件,您必须在其中添加以下设置:

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

即在 system.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>

你必须从网络服务返回一个以下结构的对象:

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文件到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;

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

【讨论】:

  • 你能给我举个WebMethod的例子吗,她应该长什么样子?因为我已经设法用 POST 这个 WebMethod 调用:[WebMethod] public string HelloWorld(string str, string str1)
  • 请您回答。这是一个客户端功能,我对此没有任何问题。我的 WebService [webmethod] 有问题,我的 webservice 方法应该如何接受此类 POST 调用?
  • @MarkoLeben:我在编辑 I 中给出了一个示例:[WebMethod] public string HelloWorld() { return "HelloWorld"; }
【解决方案2】:

来自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>

【讨论】:

  • 我已经做到了。相信我已经搜索过网络。但是我应该如何使用带有一些 xml 的 POST 方法访问它?
猜你喜欢
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 2011-03-07
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多