简体   繁体   中英

Send and Receive 'Post' data

Wonder if someone could assist me please, what I'm trying to achieve is to send data from one site to another: So in my 'sending' site I have a page that that runs the following:

    string message;
    message = "DATA DATA DATA DATA!!!";

    System.Net.WebRequest request = WebRequest.Create("http://clt-umbraco.test.clt.co.uk/storereceive/?data-response");

    request.ContentType = "application/json";
    request.Method = "POST";
    request.Headers["X-Parse-Application-Id"] = "aaaaaaaaaaaaaaa";
    request.Headers["X-Parse-REST-API-Key"] = "bbbbbbbbbbbbbbb";
    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("{\"channels\": [\"\"], \"data\": { \"alert\": \" " + message + "\" } }");
    string result = System.Convert.ToBase64String(buffer);
    Stream reqstr = request.GetRequestStream();
    reqstr.Write(buffer, 0, buffer.Length);
    reqstr.Close();

    WebResponse response = request.GetResponse();
    //jsonString.Text = response;
    reqstr = response.GetResponseStream();
    StreamReader reader = new StreamReader(reqstr);
    jsonString.Text = reader.ReadToEnd();

And on my receiving site/page I have the following on page load:

    string[] keys = Request.Form.AllKeys;
    for (int i = 0; i < keys.Length; i++)
    {
        Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
    }

I can see that this page load event is firing but the Request.Form.Allkeys object is empty where I would hope it would contain the data from the sending page. Obviously I'm going drastically wrong somewhere....could someone help please??

Thanks, Craig

That is because You are not posting a form, you are uploading raw data.

Set request.ContentType="application/x-www-form-urlencoded";

And populate your post data as such:

Key1=value1&key2=value2

You should also consider encoding the values with HttpServerUtility.UrlEncode

If you still wan't to send raw data though, then at the server end, you should read the incoming data like: (instead of Request.Form[] )

byte[] requestRawData = Request.BinaryRead(Request.ContentLength);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM