【问题标题】:System.Net.HttpWebRequest ResponseSystem.Net.HttpWebRequest 响应
【发布时间】:2017-12-01 17:24:58
【问题描述】:

需要一些帮助来解决问题。第一次这样做,我读过的所有内容似乎都没有帮助。我正在尝试从 api 调用中获取响应并在我的标签中取回结果,但我得到的只是标签中的 System.Net.HttpWebRequest,因此它至少发送和返回。 我还需要做些什么来获得预期的响应吗?

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    WebRequest req = WebRequest.Create(@"https://server.net/api/v1/.." + Id_TextBox.Text);
    req.Method = "POST";
    req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("Login:######"));
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();//(HttpWebResponse)req.GetResponse() as HttpWebResponse;
    myString_Label.Text = req.ToString();

谢谢!

【问题讨论】:

标签: c# asp.net


【解决方案1】:

你必须这样做,你也可以从 MSDN 得到同样的东西

https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.getresponsestream(v=vs.110).aspx

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream receiveStream = resp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format. 
StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");

Char[] read = new Char[256];
            // Reads 256 characters at a time.    
int count = readStream.Read( read, 0, 256 );
Console.WriteLine("HTML...\r\n");
while (count > 0) 
    {
                    // Dumps the 256 characters on a string and displays the string to the console.
        String str = new String(read, 0, count);
        Console.Write(str);
        count = readStream.Read(read, 0, 256);
    }
Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();

【讨论】:

  • 好的,抱歉我正在开会。我会尝试将其合并。
猜你喜欢
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多