简体   繁体   English

ASP.Net httpwebrequest 未发布请求正文

[英]ASP.Net httpwebrequest not posting request body

My hands are up in the air - I cry, Uncle.我的手在空中 - 我哭了,叔叔。 Help me understand:帮我理解:

I wrote a simple HttpWebRequest posting to a clients' https://website .我写了一个简单的 HttpWebRequest 发布到客户的https://website It has specific header requirements and a JSON request body.它具有特定的标头要求和 JSON 请求正文。 The GetResponseStream, after receiving a GetResponse StatusCode.OK, is an error provided by their server. GetResponseStream 在收到 GetResponse StatusCode.OK 后,是其服务器提供的错误。 They checked their logs and said while the headers were present, the request Body was ".他们检查了他们的日志并说当标题存在时,请求正文是“。

Here is my code:这是我的代码:

    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

My JSON string appears to be valid - I paste it into Fiddler, along with setting the headers, and Fiddler returns a successful login.我的 JSON 字符串似乎是有效的 - 我将它粘贴到 Fiddler 中,并设置标题,然后 Fiddler 返回成功登录。

From ASP.Net, it returns their documented error that the email address (loginId) is required.从 ASP.Net,它返回他们记录的错误,即需要电子邮件地址 (loginId)。 I don't see anything incorrect in my C# code.我在 C# 代码中没有看到任何不正确的内容。 Any ideas where else to look?任何想法在哪里看? Could the client have something on their hosting site that blocks or filters out a certain request?客户是否可以在其托管站点上阻止或过滤某个请求? I'm stuck.我被困住了。

So, for anyone who cares to know.... you must "close" the StreamWriter after performing the .Write(payload) command, otherwise it doesn't actually send the data.所以,对于任何想知道的人......你必须在执行 .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!!

I hope this helps someone else as I spend two weeks fighting this omission.我希望这能帮助其他人,因为我花了两周的时间来解决这个遗漏。 Ciao.再见。

i will do post like this:我会像这样发帖:

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(loginInfoSerialized);

request.ContentType = "application/json";
request.ContentLength = byte1.Length;
Stream stream = request.GetRequestStream();
stream.Write(byte1, 0, byte1.Length);
stream.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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