카테고리 없음

[C#] How to use CGI in C#

호마 2015. 5. 8. 17:53





예제 :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
private string TestUri(string host, string name, string pass, string query)
{
 
    UriBuilder uriBuilder = new UriBuilder();
    uriBuilder.Scheme = "http://";
    uriBuilder.Host = host;
    uriBuilder.Path = "/axis-cgi/param.cgi";
    uriBuilder.Query = query;
 
    // Line 20 => System.Net.WebException 발생 시 !!
    // 검색 : "CR 뒤에는 LF가 와야 합니다."
    // 해결 : Add property "<httpWebRequest useUnsafeHeaderParsing="true" />" in app.config
    // 위 파일은 반드시 실행(.exe) 프로젝트 아래에 포함 시켜야한다.
    WebClient webClient = new WebClient();
    if (!String.IsNullOrEmpty(name))
    {
        webClient.Credentials = new NetworkCredential(name, pass);
    }
 
    return webClient.DownloadString(uriBuilder.ToString());
}
 
private void Using()
{
    // 해당 Cgi에서 사용하는 쿼리문 정의 해놓기
    string[] myQuery = 
    {
        "action=list&group=root.Properties.Firmware.Version",   // 펌웨어 버전
        "action=list&group=Network",                            // 네트워크 정보
        "action=list&group=Properties.System.SerialNumber",     // 시리얼 번호
    };
 
    string result = TestUri(
                "192.168.1.1",  // 호스트 주소
                "root",           // 계정 
                "1234",          // 암호
                myQuery[0]
    );
    
    Console.WriteLine(result);
}
 
//
// 출력 : root.Properties.Firmware.Version=5.50.2
cs




- 서버에서 HTTP 프로토콜 위반이 커밋되었습니다.. Section=ResponseHeader Detail=CR 뒤에는 LF가 와야 합니다.

 

- Unhandled Exception: System.Net.WebException: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF



위와 같은 예외 발생시,아래 파일을 추가한다.


파일명 : App.config

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
  </system.net>
</configuration>
cs

위치 : 반드시 실행 프로그램(.exe)의 프로젝트에 추가하도록 한다.

       dll 프로젝트에 추가했다가 뻘짓..

       빌드 후,  실행 경로에서 "[실행프로그램이름].exe.config" 파일이 생성되는지 확인한다.