简体   繁体   English

如何在C#中读取响应头?

[英]How to read response headers in C#?

I have declared a class ClientClass which inherits from WebClient. 我已经声明了一个继承自WebClient的类ClientClass。 I am using this class to define an HttpWebRequest object since I want to pass SSL certificate along with the request for client authentication. 我要使用此类来定义HttpWebRequest对象,因为我想将SSL证书与客户端身份验证请求一起传递。

public class ClientClass : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        request = (HttpWebRequest)base.GetWebRequest(address);
        request.ClientCertificates.Add(GetCertificate());
        return request;
    }

    private static X509Certificate2 GetCertificate()
    {
            string certificateThumbprint = AADHelper.DecryptConfigurationSetting(ConfigHelper.CertificateThumbprint);
            string thumbprint = certificateThumbprint;
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            List<X509Certificate2> list = store.Certificates.Cast<X509Certificate2>().ToList();
            X509Certificate2 clientCertificate = list.Find(c => c.Thumbprint != null && c.Thumbprint.Equals(thumbprint, StringComparison.InvariantCultureIgnoreCase));
            return clientCertificate;
    }



}

Using UploadData Method of WebClient Class, I am able to send POST request and get response string. 使用WebClient类的UploadData方法,我能够发送POST请求并获取响应字符串。 But I want to receive response headers also along with the string. 但是我也想同时接收响应头和字符串。 I thought of using HttpWebResponse for response but I am unsure how I will be able to send post request. 我考虑过使用HttpWebResponse进行响应,但是我不确定如何发送请求。

My code for sending POST request is as follows: 我发送POST请求的代码如下:

using (var client = new ClientClass())
                {
                    client.Headers.Add("content-type", "application/json");
                      client.Headers.Add("x-ms-tracking-id", TrackingId.ToString());
                    string rawResponse;
                    rawResponse = Encoding.ASCII.GetString(client.UploadData(Url, "POST", Encoding.Default.GetBytes(jsonString)));      dataAccessLayer.InsertKeyReplacementKFSAudit(keyReplacementID, TrackingId, jsonString, rawResponse, request.User, startTime, endTime);
                    }

Could you help me with how I can read response headers as well. 您能帮助我如何读取响应标头吗? Thanks in advance. 提前致谢。

You can use client.ResponseHeaders to retrieve response headers. 您可以使用client.ResponseHeaders检索响应头。

        try
        {
            rawResponse = Encoding.ASCII.GetString(client.UploadData(Url, "POST", Encoding.Default.GetBytes(jsonString)));
            var responseHeaders = client.ResponseHeaders;
        }
        catch (WebException wex) // To handle error
        {
            // wex.Response.Headers // You can retrieve the headers
            //((HttpWebResponse)wex.Response).StatusCode //You can handle StatusCode like this;
        }

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

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