WebClient.Download 질문입니다.

조회수 3396회

유니티에서 WebClient 를 이용해서 파일을 다운로드 하려고 하는데요

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net;

public class FileDownload : MonoBehaviour {
    public string text;
    public Text logText;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        logText.text = text;
    }

    public void DownloadURL(){
        string downLink = "https://drive.google.com/open?id=0ByKk42PO-EvebmRGN0Y0V2ZSVmc";
        string filename = "C:/User/ISENDO/Desktop/Copy/deny.jpg";

        WebClient myWebClient = new WebClient ();

        try{
            myWebClient.DownloadFile(downLink,filename);        
        }

        catch(WebException error){
            text += error + "\n";
        }
    }
}

이렇게 했을 때 사진이 정상적으로 받아지지 않고 자꾸 111KB짜리 파일이 받아집니다..

뭐가 문제인건가요ㅠㅠ?

  • (•́ ✖ •̀)
    알 수 없는 사용자

2 답변

  • 다운 받으시려는 URL은 이미지만 들어있는 URL이 아니라, 이미지를 표시해 주는 웹페이지 URL이네요. 딱 ULR만 내려주는 링크가 필요합니다.

    예를들어 이미지 url을 이걸로 한 번 바꿔보시면 잘 동작할 것 같네요.

    https://res.cloudinary.com/eightcruz/image/upload/v1490000448/banner_slack_2x_iegsyt.png

    이미지 서버가 필요하시면 http://cloudinary.com/ 추천합니다. 여기에 이미지 올리고 나면 바로 받을 수 있는 URL을 얻으실 수 있을거에요.

    • 주신 url을 downLink에 넣었더니 System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff80092012 at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in :0 at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in :0 at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in :0 at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process () at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in :0 at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in :0 --- End of inner exception stack trace --- at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in :0 --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in :0 at System.Net.HttpWebRequest.GetResponse () [0x00000] in :0 at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in :0 at System.Net.WebClient.DownloadFileCore (System.Uri address, System.String fileName, System.Object userToken) [0x00000] in :0 at System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName) [0x00000] in :0 에러가 났습니다.. ㅜㅜ... 알 수 없는 사용자 2017.4.14 13:39
    • ssl 관련 오류네요. System.Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls; 를 추가해보시죠. 정영훈 2017.4.14 15:32
    • 추천해주신 이미지서버에 제가 직접 이미지와 영상 올리고 테스트해보니까 제대로 다운받는데 성공했습니다ㅎㅎ 알려주셔서 감사합니다! 알 수 없는 사용자 2017.4.14 16:45
  • public static Image getImage(String url) {
                String defaultUrl = Basic.WEB_ROOT; // 기본 URL, 원하면 수정하세요.
                String totalUrl = defaultUrl + url;
    
                // 이미지를 다운로드 합니다.
                byte[] data = new WebClient().DownloadData(totalUrl);
                MemoryStream ms = new MemoryStream(data);
                return Image.FromStream(ms);
            }
    

    개인적으로 제가 사용하는 방법입니다.

    그리고 사용중이신 URL (구글 드라이브) 는 이미지가 다이렉트 링크가 아니라 안될거 같네요.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)