dongqian1028 2016-11-20 13:00
浏览 62

使用c#将params设置为Web服务

i can't set params to my web service this is my code in php :

$myValue = $_POST['myValue'];

and this is my C# Code :

void POST(string url, string jsonContent)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes("myValue=" + jsonContent);

    request.ContentLength = byteArray.Length;
    request.ContentType = @"application/json";

    using (Stream dataStream = request.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }
    long length = 0;
    try
    {
        //request.GetResponse();
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            length = response.ContentLength;
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                string WSReturned =  reader.ReadToEnd();
            }
        }
    }
    catch (WebException ex)
    {
        //Nothing...
    }
}

i tested "&myValue=" and return null again

This is my PHP Code :

<?php

if (isset($_POST['NewArray'])) {
    $NewArray = $_POST['NewArray'];
} else {
    $NewArray = "Not";
}

echo $NewArray;?>
  • 写回答

1条回答 默认 最新

  • duanqianruan8448 2016-11-20 13:17
    关注

    If you append &myValue=Somethingto your query string you don't use $_POST to get the value but use $_GETinstead.

    Now coming to your code it seems okay to me, maybe the error is an another point of your application.

    I suggest you to check if there's a value in $_POSTwith isset, and then try to debug it.

    If you get an exception then please post the logs, hope it helps :)

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向