首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Net.HttpWebRequest响应

System.Net.HttpWebRequest响应
EN

Stack Overflow用户
提问于 2017-12-01 17:24:58
回答 1查看 663关注 0票数 0

需要一些帮助才能弄清楚。第一次这样做,我所读到的一切似乎都没有帮助。我试图从api调用中得到一个响应,并返回标签中的结果,但我得到的只是标签中的System.Net.HttpWebRequest,所以它至少会发送和返回。我还需要做些什么才能得到预期的回应吗?

代码语言:javascript
复制
    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();

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-01 17:30:14

您必须这样做,您也可以从MSDN获得相同的信息。

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

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47598452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档