获取ToString然后POST?

时间:2012-04-19 01:41:55

标签: c# wcf web-services rest

我有一个按钮,其中我正在尝试GET然后POST,我想知道是否有人可以帮助我使用下面的代码,我想要做的是将MessageID设置为字符串,以便我可以将其用于POST uriAddMessagetoGroup ...

        XDocument xDoc = XDocument.Load(uri1);
        var MessageID = xDoc.Descendants("Message")
            .Select(n => new
            {
                n.Element("MessageID").Value,
            })
            .ToString();

        string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);

完整代码:

 private void button3_Click(object sender, EventArgs e)
        {

            //Get the newly created message while the text is still in the textbox and find its messageID (int)
            string uri1 = "http://localhost:8000/Service/GetMessage/{anything}";
            string tagUri = uri1.Replace("{anything}", textBox21.Text);
            XDocument xDoc = XDocument.Load(tagUri);
            var MessageID = xDoc.Descendants("Message")
                   .Select(n => n.Element("MessageID").Value.ToString());

            //add the string to the uri and add the message to the relevant group while the group still exists in the textbox
            string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);
            byte[] arr2 = Encoding.UTF8.GetBytes(uriAddMessagetoGroup);
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(uriAddMessagetoGroup);
            req2.Method = "POST";
            req2.ContentType = "application/xml";
            req2.ContentLength = arr2.Length;
            Stream reqStrm2 = req2.GetRequestStream();
            reqStrm2.Write(arr2, 0, arr2.Length);
            reqStrm2.Close();
            HttpWebResponse resp2 = (HttpWebResponse)req2.GetResponse();
            MessageBox.Show(resp2.StatusDescription);
            reqStrm2.Close();
            resp2.Close();

2 个答案:

答案 0 :(得分:0)

尝试:

 var MessageID = xDoc.Descendants("Message")
        .Select(n =>  n.Element("MessageID").Value.ToString());

 string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);

答案 1 :(得分:0)

答案是使用FirstOrDefault()