C#建立接受像HttpWebRequest的方法POST方法websevice方法(C# build

2019-08-16 23:01发布

我需要接受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服务。 有没有人有一个想法如何做到这一点。 我在这里停留。

Answer 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回web服务,你可以设计你的愿望的一个结构近似于预期的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";
    }

[在结构的情况下,返回类型更改为对应结构类型]

以下是[根据您的需要使用方法】功能,您可以张贴到的网址,并发送一个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;

[如果这是有益的或需要更多的帮助,请让我知道]



Answer 2:

从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>


文章来源: C# build a websevice method that accepts POST methods like HttpWebRequest method