携帯からasp
サーバーにファイルをアップロードする必要があります。この機能は、クライアントがアップロードサーバー用のプロキシを追加する必要があるまで機能します。私はHttpUrlConnection
を使用android
上iOS
でAndroidの投稿のリクエストが切り捨てられました
私はNSMutableURLRequest
を使用してデータとしてHttpBody
にファイルを追加し、仕事で
let request = NSMutableURLRequest(URL:NSURL(string:urlString)!)
request.HTTPBody = uploader.createBodyWithParameters()
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {...}
、
は、私はすべてのデータを送信するように、要求が切り捨てられていました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;
}
}
}