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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Photon refusing connection on Android 0 Answers

Jittery movement of sprite on device 1 Answer

Android Downloading Packages Online 2 Answers

How to make a menu like ironpants 0 Answers

Android game crashes when attempting to start wifi hotspots 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges