簡體   English   中英

C#VS2008 HttpsClient發布數據操作方法

[英]C# VS2008 HttpsClient Post Data How-To

我正在嘗試編寫數據持續時間為300的https Post。 我剛接觸C#,正在沙盒中編寫另一個名為Crestron Simpl#的程序。 如果有人可以幫助我指出將數據添加到Post的正確方向。 謝謝

    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));                
        }       
    } 

這是一個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();

您可以在此處閱讀更多其他答案。

Crestron 3系列操作系統基於.net 3.5緊湊框架,只有擁有HTTPWebRequest類,您才可以這樣做。 但這令人討厭,就像Microsoft通過他們的httpClient類為.net 4.5中的用戶友好性抽象了HTTP一樣,我想Crestron選擇做同樣的事情並回移植或創建自己的東西。

從您的示例中,我看到您沒有設置HttpsClientRequest ContentData *字符串或字節,我認為這是您的“ postVar”

不知道您的設置變量應該是什么,我看不到您所做的事情有什么特別的問題...

此外,最好發送異步發送並在那里使用響應...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM