Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by samcsss · Oct 02, 2014 at 07:23 AM · c#androidnetworkdownload

How can I use WebClient to download files on Android when behind a proxy?

I have some very large files I need to download in my Android App. (Resource bundles and videos, some upwards of 70MB).

The problem I have when using the WWW class is that the whole file is downloaded and kept in memory before you get access to it, and this is crashing my App.

The solution I came up with was to use the System.Net.WebClient class to download and save directly to the disk.

This works great, but the only problem I have is whenever the phone is using a Proxy, the WebClient can't connect to the server (even when other web requests in my app succeed - the GoogleIAP and Flurry plugins from Prime31 work a treat)

So obviously I'm missing something in my code relating to the way I set up my WebClient, but I'm at a loss.

Here is the code I'm using:

 WebClient webClient = new WebClient();
 
 webClient.DownloadFileCompleted += new AsyncCompletedEventHandler (OnDownloadComplete);
 webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnUpdateDownloadProgress);
                 
 Uri uri = new Uri(AbsoluteURI);
 webClient.DownloadFileAsync(uri, FilePath );

AbsoluteURI and FilePath are strings, there are no problems with these values, they contain the information they should and go to the right places.

The error I get back from the WebClient is:

 I/Unity   (14191): System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused
 I/Unity   (14191):   at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0
 I/Unity   (14191):   at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x00000] in <filename unknown>:0
 I/Unity   (14191):   --- End of inner exception stack trace ---
 I/Unity   (14191):   at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
 I/Unity   (14191):   at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0

Any help is appreciated. To be honest, I'm not even 100% sure that the WebClient is the correct way to do what I want.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by samcsss · Oct 03, 2014 at 12:38 AM

Managed to solve my problem. For those who have the same problem, I query the proxy setting from Java using the AndroidJavaObject class

 AndroidJavaObject jo = new AndroidJavaObject("java.lang.System");
 string proxyHost = jo.CallStatic<string>("getProperty", "http.proxyHost");
 string proxyPort = jo.CallStatic<string>("getProperty", "http.proxyPort");
 if(string.IsNullOrEmpty(proxyHost) == false && string.IsNullOrEmpty(proxyPort) == false) 
 {
         Debug.Log("proxy host:" + proxyHost + ":" + proxyPort);
         WebProxy proxy = new WebProxy(proxyHost, int.Parse(proxyPort));
         webClient.Proxy = proxy;
 }


This seems to work for my current situation. I'm a little concerned that this will still fall down if the proxy server requires authentication.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kubasteve · Aug 14, 2015 at 10:42 PM 0
Share

Hi,

Can you example how the AndroidJavaObject works? I am trying to use webclient in my project and it works fine in ios however with android it doesnt work. I am trying to do what you do in your answer however I can't seem to get the AndroidJavaObject.

avatar image samcsss kubasteve · Apr 03, 2018 at 03:37 AM 0
Share

https://docs.unity3d.com/ScriptReference/AndroidJavaObject.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need Help - Last Problem Converting PC to Android - Using JS to read CS 2 Answers

“via app name” link not working from Facebook Android app 0 Answers

Downloading file in android background 0 Answers

Unity Multiplayer Android Game 0 Answers

Index out of Range Exception Error 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges