none
HttpWebRequest 如何传递 http数据 -d参数? RRS feed

  • 问题

  • 小白不知道下面的脚步是什么脚步

    curl -X POST \ -H "X-AVOSCloud-Application-Id: d00k2gpz7arje6sfh0hpfgm82uufr935ivdv5xku70q368ps" \ -H "X-AVOSCloud-Application-Key: wkafjtd8w1p12vxiljn9huo9q4i5tjzl0tw0xuo7aoc08j06" \ -H "Content-Type: application/json" \ -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \ https://cn.avoscloud.com/1.1/classes/GameScore

    貌似 -H是http header

    -d 参数如何传递呢?

    2014年8月28日 13:57

全部回复

  • -d是什么参数呀?
    2014年9月1日 2:21
  • 从你的code上来看,-d应该是你要传输的信息,你的代码中-d包含了score=1337的成绩,还有用户名之类的信息。

    这里有一段从MSDN摘录的如何使用HttpWebRequest的代码片段。

                // Set the 'Method' property of the 'Webrequest' to 'POST'.
                myHttpWebRequest.Method = "POST";
                Console.WriteLine ("\nPlease enter the data to be posted to the (http://www.contoso.com/codesnippets/next.asp) Uri :");
    
                // Create a new string object to POST data to the Url.
                string inputData = Console.ReadLine ();
    
    
                string postData = "firstone=" + inputData;
                ASCIIEncoding encoding = new ASCIIEncoding ();
                byte[] byte1 = encoding.GetBytes (postData);
    
                // Set the content type of the data being posted.
                myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    
                // Set the content length of the string being posted.
                myHttpWebRequest.ContentLength = byte1.Length;
    
                Stream newStream = myHttpWebRequest.GetRequestStream ();
    
                newStream.Write (byte1, 0, byte1.Length);
                Console.WriteLine ("The value of 'ContentLength' property after sending the data is {0}", myHttpWebRequest.ContentLength);
    
                // Close the Stream object.
                newStream.Close ();

    所以你对用的HttpWebRequest的方法为:

    • Post: HttpWebRequest.Method
    • Content-Type: HttpWebRequest.ContentType
    • -d: 上面代码片段中inputData的内容

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    2014年9月23日 15:44
    版主