需要一些帮助才能弄清楚。第一次这样做,我所读到的一切似乎都没有帮助。我试图从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();谢谢!
发布于 2017-12-01 17:30:14
您必须这样做,您也可以从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();https://stackoverflow.com/questions/47598452
复制相似问题