Skip to content

생계형 개발자 수첩 ver4.0

이제 나이 먹어 강제로 4.0 버전업됨, 일인개발자임, 파키스탄식 코딩.. 오래된 코드도 다시 쓴다.

C# WebException “기본 연결이 닫혔습니다”

Posted on 2017년 5월 25일 by kimczip

WebClient Class든 HttpWebRequest Class든, 간혹 같은 코드로 Web에서 데이터 수신이 잘 되던게
갑자기 안될 경우가 있다.

Exception 메시지는 “기본 연결이 닫혀 있습니다.”
Google검색으로 여러가지 해결법이 있지만, 되는것도 있고 서버에서 설정 변경으로 처리해야만 가능 할 경우가 있다.

아래 코드는 위의 에러가 생길 경우, 서버 설정변경이 어려운 상황이나 같은 URL로 브라우저에서 호출하면 이상은 없는데
유독 위 Class로 자동화 구현시 Exception 걸릴 경우, 꼼수?로 좋은 코드다.

public static string DownloadHTMLPage(string URL)
{
    string pageContent = null;
    try
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);

        httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
        httpWebRequest.Referer = "http://www.google.com/";


        httpWebRequest.Timeout = 10000;
        
        WebResponse response = httpWebRequest.GetResponse();

        Stream webStream = response.GetResponseStream();


        StreamReader streamReader = new StreamReader(webStream);

        pageContent = streamReader.ReadToEnd();

        streamReader.Close();
        webStream.Close();
        response.Close();
    }
    catch (WebException ex)
    {
        Console.WriteLine("HTTP ERROR [{0}] : {1}", URL, ex.ToString());
        return null;
    }

    return pageContent;
}
소스 보기
소스 숨김

Share the post "C# WebException “기본 연결이 닫혔습니다”"

  • Facebook
  • X
Posted in 3.0Tagged C#, Exception, Web

글 내비게이션

MySQL 덤프 파일 만들기
First Custom Cooling System

답글 남기기 응답 취소

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

버전

  • 3.0
  • 4.0

최근에..

  • 실행파일 디지털 서명하기 (프로그램 배포)
  • 자신의 PC에 막혀 있는 포트 검색
  • DB Table Column 이름 가져오기.
  • DLL 파일 PublicKeyToken 얻기
  • DSM 7.0에서 MariaDB 10 설정

Tags

10Gbps AMD Bitspower C# Command Control Corsair Crawling Exception F4-3200C14D-16GFX FLAREX G.Skill git gogs MariaDB MySQL NAS OpenCV Parallel PC-O9 rainmeter Ryzen scimitar Thermaltake UI Web WinForm 개발팁 개인사업자 광명시청 네트워크속도 데이터베이스 라이젠 랜선 랜케이블 리안리 메모리 명령프롬프트 수냉쿨링 수로 시놀로지 직구 커스텀쿨링 컴퓨터 퍼옴
Proudly powered by WordPress | Theme: micro, developed by DevriX.