2017-12-01 1 views
-2

무언가를 알아 내는데 도움이 필요합니다. 처음이 일을하고 내가 읽은 모든 것은 도움이되지 않습니다. API 호출에서 응답을 얻으려고 노력하고 내 레이블에 결과를 얻으려고하지만 System.Net.HttpWebRequest 내 레이블에 있으므로 보내고 돌아 오는 중입니다. 예상되는 응답을 되 찾으려면 내가해야 할 일이 있습니까?System.Net.HttpWebRequest Response

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(); 

감사!

+1

[C#에서의 HttpWebRequest와 HttpWebResponse 클래스]의 사용 가능한 복제 (https://stackoverflow.com/questions/ MSDN에서 동일한 얻을 수 있습니다 6723792/httpwebrequest 및 httpwebresponse-in-C-sharp) –

+0

요청 개체에서 ToString()을 호출하면 개체의 문자열 표현이 나타납니다. resp에서 ToString()을 호출하거나 응답을 객체로 역 직렬화하여 표시 할 수 있습니다. – Marisa

+0

샘플을보십시오. https://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx – lwb

답변

0

당신은 다음과 같이해야 할, 당신은 또한

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(); 
+0

죄송합니다. 회의에 참석했습니다. 나는 이것을 통합하려고 노력할 것이다. – SenorInIssaquah