0

I have only two input parameters in my feedback form, email, feedback and a submit button only. I want to send(POST) this data to a specific URL like http://questoons.com/data.php

So how can I post data from windows phone 8 app to a specific URL?

Any code sample or web link would be highly appreciated.

Vivek
  • 663
  • 2
  • 13
  • 40

2 Answers2

2

I reccomend to write a specific class for working with POST.

public class PostRequestParameters
    {
        public List<PostRequestParameterObject> prms;

        public PostRequestParameters()
        {
            prms = new List<PostRequestParameterObject>();
        }

        public void AddPair(string id, string val)
        {
            prms.Add(new PostRequestParameterObject(id, val));
        }

        public String FormPostData()
        {
            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < prms.Count; i++)
            {
                if (i == 0)
                {
                    buffer.Append(System.Net.HttpUtility.UrlEncode(prms[i].id) + "=" + System.Net.HttpUtility.UrlEncode(prms[i].value));
                }
                else
                {
                    buffer.Append("&" + System.Net.HttpUtility.UrlEncode(prms[i].id) + "=" + System.Net.HttpUtility.UrlEncode(prms[i].value));
                }
            }

            return buffer.ToString();
        }
    }

    public class PostRequestParameterObject
    {
        public string id;
        public string value;

        public PostRequestParameterObject(string id, string val)
        {
            this.id = id;
            this.value = val;
        }
    }

Then you can use it to send POST data:

private void buttonSend_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {               
                WebClient wc = new WebClient();
                wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
                wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
                wc.Encoding = Encoding.UTF8;

                PostRequestParameters prms = new PostRequestParameters();
                prms.AddPair("par1", textBox1.Text);
                prms.AddPair("par2", textBox2.Text);
                prms.AddPair("par3", GetMPar3());

                wc.UploadStringAsync(new Uri(url), "POST", prms.FormPostData(), null);

        }

        // you can parse response here
        private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            // do something
        }
Olter
  • 1,129
  • 1
  • 21
  • 40
  • though there is an error in the line starting with "wc.UploadStringCompleted" showing that you need to raise an event since it is an event and cannot be called directly – Hannington Mambo Jun 21 '16 at 18:40
1

You can try using WebClient's UploadStringAsync() function :

string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";

var wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(new Uri(URI, UriKind.Absolute), myParameters);

[For Reference]

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137