Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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
C# ASP.Net httpwebrequest未发布请求正文_C#_Asp.net_Json - Fatal编程技术网

C# ASP.Net httpwebrequest未发布请求正文

C# ASP.Net httpwebrequest未发布请求正文,c#,asp.net,json,C#,Asp.net,Json,我的手举在空中——我哭了,叔叔。请帮助我理解: 我写了一个简单的HttpWebRequest发布到客户端。 它有特定的头需求和JSON请求体。GetResponseStream在收到GetResponse StatusCode.OK后,是其服务器提供的错误。他们检查了自己的日志,并说,当标题出现时,请求主体是” 这是我的密码: public const string API_URILogin = "https://dev.clientURL.com/test/api/login";

我的手举在空中——我哭了,叔叔。请帮助我理解:

我写了一个简单的HttpWebRequest发布到客户端。 它有特定的头需求和JSON请求体。GetResponseStream在收到GetResponse StatusCode.OK后,是其服务器提供的错误。他们检查了自己的日志,并说,当标题出现时,请求主体是”

这是我的密码:

    public const string API_URILogin = "https://dev.clientURL.com/test/api/login";
    private void RequestTest()
    {
        LoginAPIStruct loginInfo = new LoginAPIStruct();

        loginInfo.loginId = TestId;
        loginInfo.password = TestPw;
        loginInfo.vehicleType = "E";
        loginInfo.deviceId = "ABCDEF";
        loginInfo.clientId = "123456";
        loginInfo.appVersion = string.Empty;
        loginInfo.osType = string.Empty;
        loginInfo.osVersion = string.Empty;

        // Call LoginAPI
        string loginStatus = LoginAPI(loginInfo);
        //Display Results
        lblResults.Text = loginStatus;
    }


    // Public Variables
    public static string errorMessage;

    // ** Client Login **
    public static string LoginAPI(LoginAPIStruct loginInfo)
    {
        // Post Login to Client.  Get response with Vehicle structure.  
        string loginInfoSerialized = string.Empty;
        string returnString = string.Empty;

        loginInfoSerialized = new JavaScriptSerializer().Serialize(loginInfo);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_URILogin);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.Headers.Add("From", "CC");
        request.Headers.Add("Language", "0");
        request.Headers.Add("Offset", "-8");
        request.Headers.Add("To", "CCM");

        // Post Request
        using (StreamWriter streamPost = new StreamWriter(request.GetRequestStream()))
        {
            streamPost.Write(loginInfoSerialized);
        }

        // Get Response
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            // Status Okay.
            //string responseStatusDescription = response.StatusDescription;

            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                try
                {
                    var result = streamReader.ReadToEnd();

                    returnString = result;
                }
                catch (Exception ex)
                {
                    returnString = ex.Message;
                }

            }
        }
        else
        {
            returnString = response.StatusCode.ToString();
        }

        return returnString;
    }   // end LoginAPI
我的JSON字符串似乎是有效的——我将其粘贴到Fiddler中,并设置标题,Fiddler返回一个成功登录


从ASP.Net返回他们的文档错误,即电子邮件地址(loginId)是必需的。我没有发现我的C代码中有任何不正确的地方。有什么其他的想法吗?客户端的托管站点上有什么东西可以阻止或过滤某个请求吗?我被卡住了。

所以,对于任何想知道的人来说……你必须”执行.Write(payload)命令后关闭StreamWriter,否则它实际上不会发送数据

    // Post Request
        StreamWriter streamPost = new StreamWriter(request.GetRequestStream());
        streamPost.Write(loginInfoSerialized);
        streamPost.Close();    //Don't forget to Close() or no data sent!!

我希望这对其他人有所帮助,因为我花了两周时间与这一遗漏作斗争。再见。

更新:我发现了如何配置ASP.net以让Fiddler充当代理。GlobalProxySelection.Select=new WebProxy(“127.0.0.1”,8888);我看到了我的标题,但内容长度设置为0,并且没有发送任何内容。为什么?与其手动关闭流,不如使用
using
语句,因为StreamWriter实现IDisposable接口,一旦程序退出
using
语句,就会自动处理流