Skip to content

생계형 개발자 수첩 ver4.0

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

Tag: Web

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;
}
소스 보기
소스 숨김
Posted in 3.0Tagged C#, Exception, WebLeave a comment

C# WebClient를 이용한 URL Post 전송

Posted on 2017년 5월 23일 by kimczip

웹페이지에서 보안이나 기타이유로 URL 파라미터를 Get 전송방식을 막아놓을 때가 있다.

그래서 Post 방식으로 정보를 전달 할 때 아래와 같이 간단하게 구현 가능하다.

 

using System.Net;
using System.Collections.Specialized;


// Web Client Class의 UploadValues Method 이용
WebClient = client = new WebClient();

string url = "http://.......";
NameValueCollection params = new NameValueCollection
{
    { "PARAMETER_NAME_1", "PARAMETER_VALUE_1"},
    { "PARAMETER_NAME_2", "PARAMETER_VALUE_2"},
    { "PARAMETER_NAME_3", "PARAMETER_VALUE_3"}
};

byte[] response = client.UploadValues(url, params);

Console.WriteLine(Encoding.UTF8.GetString(response));



// 활용안
        public static byte[] Post(string uri, NameValueCollection pairs)
        {
            byte[] response = null;
            using (WebClient client = new WebClient())
            {
                try
                {
                    response = client.UploadValues(uri, pairs);
                }
                catch(WebException ex)
                {
                    Console.WriteLine("ERROR [ {0} ] => {1}", uri, ex.Message);
                }
            }
            return response;
        }

 

소스 보기
소스 숨김
Posted in 3.0Tagged C#, WebLeave a comment

버전

  • 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.