Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过POST将C#变量发送到PHP_C#_Http_Post - Fatal编程技术网

通过POST将C#变量发送到PHP

通过POST将C#变量发送到PHP,c#,http,post,C#,Http,Post,我正在做一个项目,我需要将变量作为帖子发送到PHP脚本 C#调用一些方法来设置字符串变量,然后我需要将这些变量发布到PHP脚本中,以便PHP脚本可以使用变量中的数据 我怎么做呢?我试过谷歌,但它们看起来都有点复杂,还有如何从PHP获取数据,这在示例中很难确定post从何处开始和结束 我需要知道的只是如何将数据发布到脚本中,而不是让C#读取PHP脚本所做的事情 谢谢你能提供的帮助 更新 我认为我没有很好地解释我想要实现的目标 其目的是通过post将字符串格式的数据发送到PHP页面。然后我想将脚本加

我正在做一个项目,我需要将变量作为帖子发送到PHP脚本

C#调用一些方法来设置字符串变量,然后我需要将这些变量发布到PHP脚本中,以便PHP脚本可以使用变量中的数据

我怎么做呢?我试过谷歌,但它们看起来都有点复杂,还有如何从PHP获取数据,这在示例中很难确定post从何处开始和结束

我需要知道的只是如何将数据发布到脚本中,而不是让C#读取PHP脚本所做的事情

谢谢你能提供的帮助

更新 我认为我没有很好地解释我想要实现的目标

其目的是通过post将字符串格式的数据发送到PHP页面。然后我想将脚本加载到用户的web浏览器中,他们将能够看到php页面,当他们从php页面提交表单时,它可以使用发送给它的帖子中的信息

下面是将要发生的事情的一步一步的介绍

  • 用户按下C#程序中的按钮
  • C#程序设置字符串变量
  • C#向PHP页面发送帖子
  • 在用户浏览器中加载相同的php页面
  • 当页面加载时,它会将来自c#的post数据处理为变量,以便可以使用这些数据
  • 用户提交表单,处理后的post数据与表单post数据一起使用
  • 希望这更有意义,这就是为什么我说我不希望C#返回PHP脚本所做的事情,因为用户需要能够使用PHP页面

    看看这个:

    public static string Post(string service, IDictionary<string, string> objects)
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(ServiceAdress+service+".php");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
    
            StringBuilder b= new StringBuilder();
            foreach(KeyValuePair<string,string>  o in objects)
                b.Append(HttpUtility.UrlEncode(o.Key)).Append("=").Append(HttpUtility.UrlEncode(o.Value??"")).Append("&");
            if (PHPSESSID != null)
                b.Append("PHPSESSID=").Append(PHPSESSID).Append('&');
    
            string postData = b.ToString(0, b.Length - 1);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
    
            if (((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
                return null;
    
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine(responseFromServer);
            reader.Close();
    
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }
    
    publicstaticstringpost(字符串服务、IDictionary对象)
    {
    //使用可以接收帖子的URL创建请求。
    WebRequest=WebRequest.Create(servicedress+service+“.php”);
    //将请求的Method属性设置为POST。
    request.Method=“POST”;
    //创建POST数据并将其转换为字节数组。
    StringBuilder b=新的StringBuilder();
    foreach(对象中的KeyValuePair o)
    b、 Append(HttpUtility.UrlEncode(o.Key)).Append(“=”).Append(HttpUtility.UrlEncode(o.Value??”).Append(“&”);
    if(PHPSESSID!=null)
    b、 追加(“PHPSESSID=).Append(PHPSESSID).Append('&');
    字符串postData=b.ToString(0,b.长度-1);
    byte[]byteArray=Encoding.UTF8.GetBytes(postData);
    request.ContentType=“application/x-www-form-urlencoded”;
    request.ContentLength=byteArray.Length;
    Stream dataStream=request.GetRequestStream();
    写入(byteArray,0,byteArray.Length);
    dataStream.Close();
    WebResponse=request.GetResponse();
    if(((HttpWebResponse)response.StatusCode!=HttpStatusCode.OK)
    返回null;
    dataStream=response.GetResponseStream();
    StreamReader=新的StreamReader(数据流);
    字符串responseFromServer=reader.ReadToEnd();
    Console.WriteLine(responseFromServer);
    reader.Close();
    dataStream.Close();
    response.Close();
    返回responseFromServer;
    }
    
    您可以使用WebRequest/WebResponse来完成。删除的php标记与此无关。对方的语言并不重要。这只是普通的HTTP调用。而且,你知道,你不能在程序之外的任何地方发送变量。你只能发送一个字符串。