네트워크 연결 정보를 검색하는 방법(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

이 항목에서는 Windows.Networking.Connectivity 네임스페이스의 클래스를 사용하여 장치의 네트워크 연결에 대한 연결 세부 정보 및 사용 정보를 검색하는 방법을 보여 줍니다.

사전 요구 사항

다음 예는 JavaScript를 사용하며 네트워크 정보 샘플을 기반으로 합니다. JavaScript를 사용하여 Windows 런타임 앱을 만드는 방법에 대한 일반적인 내용은 JavaScript를 사용하여 첫 번째 Windows 런타임 앱 만들기를 참조하세요.

연결 프로필이란?

ConnectionProfile은 장치에 설정된 단일 네트워크 연결을 나타냅니다. ConnectionProfile에서 액세스하는 정보를 사용하여 현재 연결 수준을 확인하거나, 데이터 사용량을 추적하거나, 연결을 유지 관리하는 데 사용되는 네트워크 어댑터를 식별할 수 있습니다. ConnectionProfile의 속성 변경에 대한 알림을 받도록 등록하면 연결된 Windows 런타임 앱에서 네트워크 환경의 변경에 대해 동작을 적용할 때 적절하게 선택할 수 있습니다. 이러한 알림 등록에 대한 자세한 내용은 네트워크 연결 이벤트 및 가용성 변경을 관리하는 방법을 참조하세요.

종종 로밍되고 데이터 통신 연결 네트워크에서 작동하는 모바일 장치에 대한 연결된 응용 프로그램과 같이 특별한 시나리오의 경우 ConnectionProfile은 예기치 않은 통신 회사 서비스 요금을 방지하는 데 사용할 수 있는 비용 및 데이터 요금제 정보를 제공합니다. 자세한 내용은 데이터 통신 연결 네트워크 비용 제약 조건을 관리하는 방법을 참조하세요.

ConnectionProfile은 다음과 같은 연결 정보에 대한 액세스를 제공합니다.

데이터 이 데이터를 제공하는 요소 설명

연결 비용

ConnectionCost

데이터 제한 및 로밍 정보를 포함한 비용 정보입니다.

비용 유형

NetworkCostType

연결에서 현재 사용하는 네트워크의 비용 유형입니다.

데이터 요금제 상태 및 사용량

DataPlanStatus, DataPlanUsage

연결과 관련된 데이터 요금제에 대한 사용량 정보입니다.

로컬 사용량

NetworkUsage

로컬 연결 사용량 정보입니다.

네트워크 어댑터

NetworkAdapter

연결을 제공하는 네트워크 어댑터입니다.

WLAN 및 WWAN 연결 속성

WlanConnectionProfileDetails

WwanConnectionProfileDetails

WLAN 및 WWAN 연결 프로필과 관련된 추가 정보를 제공합니다.

 

연결 프로필 검색

먼저 앱이 NetworkInformation을 검색하는 데 사용하는 메서드를 정의하는 ConnectionProfile 클래스 인스턴스를 정의합니다. 그런 다음 NetworkCostType과 연결된 가능한 네트워크 비용 유형을 정의하는 ConnectionProfile 클래스 인스턴스를 정의합니다.

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
var networkCostInfo = Windows.Networking.Connectivity.NetworkCostType;

NetworkInformation 클래스는 ConnectionProfile 검색을 위한 두 가지 메서드를 정의합니다. 인터넷 연결과 관련된 프로필만 반환해야 하는 경우 getInternetConnectionProfile 메서드를 사용합니다.

따라서 대부분의 비동기 네트워크 메서드를 호출할 때 예외를 처리하는 코드를 작성해야 합니다. 또한 ConnectionProfile을 검색하는 Windows.Networking.Connectivity 네임스페이스의 메서드에서 예외가 발생할 수 있습니다. 예외 처리기는 예외의 원인에 대해 보다 자세한 정보를 검색하므로 오류를 더 잘 이해하고 적절한 의사 결정을 내릴 수 있습니다. 자세한 내용은 네트워크 앱에서 예외를 처리하는 방법을 참조하세요.

function displayInternetConnectionProfileInfo() {
    try {
        // get the ConnectionProfile that is currently used to connect to the Internet
        var internetProfile = networkInfo.getInternetConnectionProfile();
        mySample.displayStatus(GetConnectionProfileInfo(internetProfile));
    }
    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }
}

인터넷 연결을 포함하여 모든 연결에 대한 프로필을 검색하려면 getConnectionProfiles 메서드를 사용합니다.

function displayConnectionProfileList() {
  var profileList = "";
  try {
      var ConnectionProfiles = networkInfo.getConnectionProfiles();
      if (ConnectionProfiles.length != 0) {
          for (var i = 0; i < ConnectionProfiles.length; i++) {

              //Display Connection profile info for each profile
              profileList += GetConnectionProfileInfo(ConnectionProfiles[i]);
              profileList += "-----------------------------------------\n\r";
          }
          mySample.displayStatus(profileList);
          }
          else {
              mySample.displayStatus("No profiles found");
          }
      }

      catch (e) {
         mySample.displayError("Exception Caught: " + e + "\n\r");
      }
}

연결 프로필에서 정보 액세스

다음 예제 코드에서는 ConnectionProfile의 메서드를 호출하여 네트워크 연결 상태, 비용 및 데이터 요금제 사용 정보를 검색합니다.

function getConnectionProfileInfo(connectionProfile) {

    if (connectionProfile == null) {
        return "";
    }

    try {
        var returnString = "ProfileName: " + connectionProfile.profileName + "\n\r";

        switch (connectionProfile.getNetworkConnectivityLevel()) {
            case networkConnectivityInfo.none:
                returnString += "Connectivity Level: None\n\r";
                break;
            case networkConnectivityInfo.localAccess:
                returnString += "Connectivity Level: Local Access\n\r";
                break;
            case networkConnectivityInfo.constrainedInternetAccess:
                returnString += "Connectivity Level: Constrained Internet Access\n\r";
                break;
            case networkConnectivityInfo.internetAccess:
                returnString += "Connectivity Level: Internet Access\n\r";
                break;
        }

        //Display Connection cost info
        returnString += "Connection Cost Information:\n\r";
        returnString += "===============\n\r";
        var connectionCost = connectionProfile.getConnectionCost();
        returnString += "Cost Type: " + GetCostType(connectionCost.networkCostType) + "\n\r";
        returnString += "Roaming: " + connectionCost.roaming + "\n\r";
        returnString += "Over Datalimit: " + connectionCost.overDataLimit + "\n\r";
        returnString += "Approaching Datalimit: " + connectionCost.approachingDataLimit + "\n\r";

        //Display Dataplan status info
        returnString += "Dataplan Status Information:\n\r";
        returnString += "===============\n\r";
        var dataPlanStatus = connectionProfile.getDataPlanStatus();
        if (dataPlanStatus.dataPlanUsage != null) {
            returnString += "Usage In Megabytes: " + dataPlanStatus.dataPlanUsage.megabytesUsed + "\n\r";
            returnString += "Last Sync Time: " + dataPlanStatus.dataPlanUsage.lastSyncTime + "\n\r";
        }
        else {
            returnString += "Dataplan Usage: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.InboundBitsPerSecond != null) {
            returnString += "Inbound Bits Per Second: " + dataPlanStatus.InboundBitsPerSecond + "\n\r";
        }
        else {
            returnString += "Inbound Bits Per Second: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.OutboundBitsPerSecond != null) {
            returnString += "Outbound Bits Per Second: " + dataPlanStatus.OutboundBitsPerSecond + "\n\r";
        }
        else {
            returnString += "Outbound Bits Per Second: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.dataLimitInMegabytes != null) {
            returnString += "Data Limit In Megabytes: " + dataPlanStatus.dataLimitInMegabytes + "\n\r";
        }
        else {
            returnString += "Data Limit In Megabytes: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.nextBillingCycle != null) {
            returnString += "Next Billing Cycle: " + dataPlanStatus.nextBillingCycle + "\n\r";
        }
        else {
            returnString += "Next Billing Cycle: " + "Not Defined" + "\n\r";
        }

        if (dataPlanStatus.maxDownloadFileSizeInMegabytes != null) {
            returnString += "Maximum Download File Size in Megabytes: " + dataPlanStatus.maxDownloadFileSizeInMegabytes + "\n\r";
        }
        else {
            returnString += "Maximum Download File Size in Megabytes: " + "Not Defined" + "\n\r";
        }
            returnString += "Cost Based Suggestions: " + CostBasedSuggestions(connectionCost) + "\n\r";
        }

    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }

    return returnString;
}

요약

이 항목에서는 연결 프로필 및 각 프로필에 포함된 연결 정보를 검색하는 방법을 살펴보았습니다. 이 정보를 사용하여 앱이 올바른 선택을 하도록 하는 것은 안정적인 연결 환경을 위해 필수적입니다.

연결 정보를 사용하여 네트워크 앱의 동작을 안내하는 자세한 내용 및 모범 사례를 보려면 네트워크 연결 이벤트 및 가용성 변경을 관리하는 방법을 참조하세요.

관련 항목

기타

JavaScript를 사용하여 첫 Windows 런타임 앱 만들기

네트워크 앱에서 예외를 처리하는 방법

네트워크 연결 이벤트 및 가용성 변경을 관리하는 방법

데이터 통신 연결 네트워크 비용 제약 조건을 관리하는 방법

네트워크 어댑터 및 위치 정보를 검색하는 방법

네트워크 연결 사용 데이터를 검색하는 방법

참조

ConnectionProfile

DataPlanStatus

NetworkInformation

Windows.Networking.Connectivity

샘플

네트워크 정보 샘플

네트워크 상태 배경 샘플