2016-12-18 2 views
1

Мне нужно загрузить файл в asp сервер с мобильного. Эта функция работает до тех пор, пока клиент не захочет добавить прокси для сервера загрузки.Андроид был усечен

На iOS я использовал NSMutableURLRequest и добавить файл в HttpBody в качестве данных,

let request = NSMutableURLRequest(URL:NSURL(string:urlString)!) 
request.HTTPBody = uploader.createBodyWithParameters() 
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {...} 

Какой работы,

на android я использовал HttpUrlConnection, я нашел запрос был усечен, что все данные, отправить по OutputStream исчезает.

URL connectURL = new URL(serverURL); 
      HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); 

      conn.setDoInput(true); // Allow Inputs 
      conn.setDoOutput(true); // Allow Outputs 
      conn.setUseCaches(false); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Accept", "*/*"); 
      conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); 
      conn.setRequestProperty("Accept-Language", "en-usa"); 

      conn.addRequestProperty("Authorization", "Bearer " + uploader.accesstoken); 
      conn.setRequestProperty("Content-Type", 
        "multipart/form-data; boundary=" + boundary); 


      DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 

      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" 
          + fileName +"\"" + lineEnd); 

      dos.writeBytes("Content-Type: "+"application/zip, application/octet-stream"+"\r\n\r\n"); 

      FileInputStream fileInputStream = new FileInputStream(new File(filePath+"/"+fileName)); 
      int bytesAvailable = fileInputStream.available(); 
      int maxBufferSize = 1024 * 1024; 

      int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      byte[]buffer = new byte[bufferSize]; 

      // read file and write it into form... 
      int bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) { 

       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      } 

      // send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

Вот IOS данные запроса

POST /Upload.ashx HTTP/1.1 
Connection: keep-alive 
Content-Length: 6998 
Content-Type: multipart/form-data; boundary=Boundary-417BEF5E-9840-4369-B397-C5BA87C25D9E 
Accept: */* 
Accept-Encoding: gzip, deflate 
Accept-Language: en-usa 
Authorization: ... 
Host: 192.168.1.5 
User-Agent: Apploader/11 CFNetwork/758.2.8 Darwin/15.6.0 

--Boundary-417BEF5E-9840-4369-B397-C5BA87C25D9E 
Content-Disposition: form-data; name="file"; filename="..." 
Content-Type: application/zip, application/octet-stream 

.......  
--Boundary-417BEF5E-9840-4369-B397-C5BA87C25D9E-- 

Android конец от "User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G925V Сложение/MMB29K)"

POST /AppLoader/Upload.ashx HTTP/1.1 
Connection: Keep-Alive 
Content-Length: 7464126 
Content-Type: multipart/form-data; boundary=Boundary-fc13ac98-53bf-4f3f-9a56-ddc4393d8719 
Accept-Encoding: gzip 
Authorization: Bearer .. 
Host: ... 
User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G925V Build/MMB29K) 

Я уверен, что проблема, возникающая при добавлении прокси, без прокси-сервера, является функцией загрузки. Мой вопрос заключается в том, что отличается между iOS и Android, что делает загрузку андроида неудачной, как это исправить?

здесь прокси-код:

using Client.Proxy.Controllers; 
using Client.Utilites; 
using System; 
using System.Diagnostics; 
using System.IO; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Http; 

namespace Proxy.Controllers 
{ 
    [RoutePrefix("Loader")] 
    public class AppLoaderController : BaseApiController 
    { 
     public AppLoaderController() 
     { 
      base.defaultServiceProxyName = "Loader"; 
     } 
     [HttpPost] 
     [ActionName("Upload.ashx")] 
     public HttpResponseMessage Upload() 
     { 
      HttpResponseMessage result = new HttpResponseMessage(); 
      try 
      { 
       LoggingUtilities.WriteLog("Upload Started", TraceEventType.Verbose); 
       var connector = GetConnectorProxyURI("AppLoaderReceiver").ToString();     
       LoggingUtilities.WriteLog(string.Format("Connector: {0}", connector), TraceEventType.Verbose); 

       string requestUri = string.Format("{0}/Upload.ashx", connector.Replace("/AppLoader", "/")); 
       LoggingUtilities.WriteLog(string.Format("requestUri: {0}", requestUri), TraceEventType.Verbose); 

       MultipartFormDataContent content = new MultipartFormDataContent(); 
       byte[] buffer = null; 
       try 
       { 
        HttpPostedFile file; 
        string requestFileName = string.Format("request-{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now); 
        LoggingUtilities.WriteLog(string.Format("log request to file."), TraceEventType.Verbose); 
        HttpContext.Current.Request.SaveAs(string.Format(@"c:\temp\{0}", requestFileName), true); 
        LoggingUtilities.WriteLog(string.Format("About to check Current.Request.Files.Count"), TraceEventType.Verbose); 
        if (HttpContext.Current.Request.Files.Count > 0) 
        { 
         file = HttpContext.Current.Request.Files[0]; 
         LoggingUtilities.WriteLog(string.Format("file.ContentLength ={0}", file.ContentLength), TraceEventType.Verbose); 
         using (BinaryReader reader = new BinaryReader(file.InputStream)) 
         { 
          buffer = reader.ReadBytes(file.ContentLength); 
         } 
         LoggingUtilities.WriteLog("Got FileData", TraceEventType.Verbose); 
         content.Add(new ByteArrayContent(buffer, 0, buffer.Length), "application/zip", file.FileName); 
         LoggingUtilities.WriteLog("Got ByteArrayContent", TraceEventType.Verbose); 
         string fileName = ""; 
         fileName = HttpContext.Current.Request.Files[0].FileName; 
         LoggingUtilities.WriteLog(string.Format("Filename= {0}", fileName), TraceEventType.Verbose); 
         if (string.IsNullOrEmpty(fileName)) 
         { 
          TimeSpan span = (TimeSpan)(DateTime.UtcNow - new DateTime(0x7b1, 1, 1)); 
          string str3 = span.TotalMilliseconds.ToString().Substring(0, 13); 
          fileName = string.Format("*.zip", str3); 
         } 
         content.Add(new StringContent(fileName), "FILE_NAME"); 
         Task<HttpResponseMessage> task1 = new HttpClient().PostAsync(requestUri, content); 
         task1.Wait(); 
         result = task1.Result; 
        } 
        else 
        { 
         result = new HttpResponseMessage(HttpStatusCode.BadRequest); 
        } 
       } 
       catch (Exception exception) 
       { 
        result = new HttpResponseMessage(HttpStatusCode.BadRequest); 
       } 
      } 
      catch (Exception exception2) 
      { 
       result = new HttpResponseMessage(HttpStatusCode.BadRequest); 
      } 
      return result; 
     } 
    } 
} 

ответ

0

How do I make HttpURLConnection use a proxy? этот ответ может помочь вам; В андроиде есть много http-инструментов, таких как volley, aysnchttp, okhttp ... вы можете выбрать один из них вместо оригинального HttpUrlConnection; , мы можем узнать больше об этой проблеме, если вы можете добавить лог-печать в код и предоставить нам журнал;

0

Спасибо Крис и Гао. я установил этот вопрос Вытащите

conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); 

Какой эффект сервера преобразования текстового сообщения GZIP до его отправки в Android.

Другая проблема возникает из-за того, что я получил -1 из conn.getContentLength(), Я думал, что контент пуст, но это только средства запроса имеют неизвестную длину содержимого. все еще может читать содержимое из conn.getInputStream().

Смежные вопросы