C#VS2008 HttpsClient发布数据方法

时间:2016-07-27 14:48:41

标签: c#

我正在尝试编写一个数据持续时间= 300的https帖子。我是C#的新手,我正在沙盒中为另一个名为Crestron Simpl#的程序写作。如果有人能帮我指出正确的方向将数据添加到邮政。感谢

    public void post(String postVar)
    {
        try
        {
            var httpsSet = new HttpsClient();
            httpsSet.KeepAlive = false;
            httpsSet.Accept = "application/xml";
            httpsSet.UserName = username;
            httpsSet.Password = password;
            httpsSet.HostVerification = false;
            httpsSet.PeerVerification = false;
            HttpsClientRequest sRequest = new HttpsClientRequest();
            sRequest.RequestType = RequestType.Post;
            sRequest.Url.Parse("https://" + ipaddress + postVar);
            HttpsClientResponse response = httpsSet.Dispatch(sRequest);
            string responseRx = response.ContentString;
            ushort iRepsonse = myRx(responseRx); 
        }
        catch (Exception e)
        {
            CrestronConsole.PrintLine(String.Format("{0} exception", e.Message));                
        }       
    } 

2 个答案:

答案 0 :(得分:0)

以下是PUT请求的示例C#代码段。

HttpWebRequest HttpWReq = (HttpWebRequest) WebRequest.Create("http:\\YourDomain.com");
ASCIIEncoding Encoding = new ASCIIEncoding();
byte[] data = Encoding.GetBytes("Put you data here");
HttpWReq.Method = "PUT";
HttpWReq.ContentType = "Enter your MIME type";
HttpWReq.ContentLength = data.Length;

//Then when you actually want to write the request, perform these functions:
Stream NewStream = HttpWReq.GetRequestStream();
NewStream.Write(data, 0, data.Length);
NewStream.Close();

您可以阅读更多其他答案here

答案 1 :(得分:0)

快思聪3系列操作系统基于.net 3.5紧凑型框架,只有HTTPWebRequest级才能以这种方式完成。 但是,正如微软在.net 4.5中使用他们的httpClient类提取用户友好性的http一样令人讨厌,我想Crestron选择做同样的事情,然后移植或创建他们自己的。

从你的例子中我看到你没有设置HttpsClientRequest ContentData *字符串或字节,我相信这将是你的" postVar"

不知道你的设置变量应该是什么我没看到你做什么的特定问题...

此外,您最好发送异步调度并在那里使用响应...