c# 无法从 GetResponse() 获得响应 headers
c# cannot getting response headers from GetResponse()
这是我向服务器发送请求的 C# 代码:
try
{
string url = "https://example.com/";
string json = "thisisanexample";
byte[] data = Encoding.UTF8.GetBytes(json);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.Accept = "application/json, text/plain, */*";
request.Headers.Add("Accept-Language", "en-US,en;q=0.8");
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
ServicePointManager.Expect100Continue = false;
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (data, 0, data.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
return responseFromServer;
}
catch (Exception e)
{
return "ERROR:" + e.Message;
}
问题是没有得到响应 headers...我试过使用 GetResponse.Headers(),但没有用...请帮帮我(我厌倦了使用此代码 5 天)...
这是我向服务器发送请求的 C# 代码:
try
{
string url = "https://example.com/";
string json = "thisisanexample";
byte[] data = Encoding.UTF8.GetBytes(json);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.Accept = "application/json, text/plain, */*";
request.Headers.Add("Accept-Language", "en-US,en;q=0.8");
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
ServicePointManager.Expect100Continue = false;
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (data, 0, data.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
return responseFromServer;
}
catch (Exception e)
{
return "ERROR:" + e.Message;
}
问题是没有得到响应 headers...我试过使用 GetResponse.Headers(),但没有用...请帮帮我(我厌倦了使用此代码 5 天)...