开发者

Receiving post from C# into PHP

Here's what I'm trying to do. I have a program set up (it works and even posts to a demo page that I don't have the code to and returns the correct values back, will post the code shortly) and with the code I am using, I can't find any information on how to read the post from PHP and return the variables back to c#(for the testing phase). My eventual goal is to gather the information form the post and send it over to the rest of the pages on the same site as the getpost.php page. If somebody could help me with this I'd be much appreciative.

class WebPostRequest
{
    WebRequest theRequest;
    HttpWebResponse theResponse;
    ArrayList theQueryData;

    public WebPostRequest(string url)
    {
        theRequest = WebRequest.Cre开发者_开发问答ate(url);
        theRequest.Method = "POST";
        theQueryData = new ArrayList();
    }

    public void Add(string key, string value)
    {
        theQueryData.Add(String.Format("{0}={1}", key, System.Web.HttpUtility.UrlEncode(value)));
    }

    public string GetResponse()
    {
        // Set the encoding type
        theRequest.ContentType = "application/x-www-form-urlencoded";

        // Build a string containing all the parameters
        string Parameters = String.Join("&", (String[])theQueryData.ToArray(typeof(string)));
        theRequest.ContentLength = Parameters.Length;

        // We write the parameters into the request
        StreamWriter sw = new StreamWriter(theRequest.GetRequestStream());
        sw.Write(Parameters);
        sw.Close();

        // Execute the query
        theResponse = (HttpWebResponse)theRequest.GetResponse();
        StreamReader sr = new StreamReader(theResponse.GetResponseStream());
        return sr.ReadToEnd();
    }

}

Like I said, on the test page, everything is working fine, the posts are getting received into the test page and transmitted back to my program.

***UPDATE:::

Ok, I got the php on my site to gather the POST information from the C# Application. Now I need to forward that information over to and redirect the user to my other webpage so that they may finish their session and accomplish the reason for them to be at my site.


The PHP and the post data from others sources sometimes behave in strange ways. To locate yout problem you can do soemthing like this:

$post_from_c = json_decode(trim(file_get_contents('php://input')), true);

file_get_contents('php://input') it means that you get the raw data rather than the processed data by apache.


I figured it out. What I did was collected the post in a PHP and sent it over to a temporary database and call the information from the page the client was already on using the IP and a client password to access (as to eliminate multiple computers on the same IP address) then I created a cron job to erase all data after a certain time, in my case I set it to 3 hours since nobody should be on the site for that time anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜