public SetRequestHeader ( string name, string value ) : void | ||
name | string | |
value | string | |
Résultat | void |
public UnityRequest(UnityHttpClientV2 inst, string url, HttpRequest request, object previousUserData, int requestId) : base(inst, request) { self = inst; OriginalRequest = request; RequestId = requestId; PreviousUserData = previousUserData; Request = new UnityWebRequest(url); // Auto-choose HTTP method Request.method = request.Method ?? (request.Body != null ? "POST" : "GET"); // TODO Missing functionality (currently unsupported by UnityWebRequest). // req.SetRequestHeader("User-agent", request.UserAgent); // req.keepAlive = true; foreach (var pair in request.Headers) { Request.SetRequestHeader(pair.Key, pair.Value); } if (OriginalRequest.Body != null) { UploadHandler uploader = new UploadHandlerRaw(OriginalRequest.Body); if (ContentType != null) uploader.contentType = ContentType; Request.uploadHandler = uploader; } Request.downloadHandler = new DownloadHandlerBuffer(); }
public static UnityWebRequest Post(string uri, WWWForm formData) { UnityWebRequest request = new UnityWebRequest(uri, "POST") { uploadHandler = new UploadHandlerRaw(formData.data), downloadHandler = new DownloadHandlerBuffer() }; foreach (KeyValuePair<string, string> pair in formData.headers) { request.SetRequestHeader(pair.Key, pair.Value); } return request; }
/// <summary> /// 下载文件 /// </summary> /// <param name="fileName"></param> /// <param name="fileMode">Append为断点续传,Create为新建文件</param> /// <param name="remoteFileSize">Append模式下必传,远端文件大小</param> /// <param name="remoteLastModified">Append模式下必传,远端文件最后修改日期</param> /// <returns></returns> public static IEnumerator DownloadFile(string fileName, OnDownloadFileFinished onDownloadFileFinidhed, FileMode fileMode = FileMode.Create, long remoteFileSize = 0, DateTime remoteLastModified = new DateTime()) { string url = BaseDownloadingURL + fileName; string filePath = Path.Combine(AssetBundleUtility.LocalAssetBundlePath, fileName); using (UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) { if (File.Exists(filePath) && fileMode == FileMode.Append) { FileInfo localFileInfo = new FileInfo(filePath); bool isOutdate = remoteLastModified > localFileInfo.LastWriteTime; if(localFileInfo.Length == remoteFileSize && !isOutdate)//已下载完成 { onDownloadFileFinidhed(fileName, null); yield break; } if (localFileInfo.Length < remoteFileSize && !isOutdate)//继续下载 { request.downloadHandler = new DownloadHandlerFile(filePath, FileMode.Append); request.SetRequestHeader("Range", string.Format("bytes={0}-", localFileInfo.Length)); } else//重新下载 { request.downloadHandler = new DownloadHandlerFile(filePath); } } else { request.downloadHandler = new DownloadHandlerFile(filePath); } CurrentRequest = request; yield return request.Send(); string error = request.isError ? string.Format("DownloadFile Failed - url: {0}, responseCode: {1}, error: {2}", url, request.responseCode, request.error) : null; onDownloadFileFinidhed(fileName, error); } CurrentRequest = null; }
/// <summary> /// <para>Create a UnityWebRequest configured to send form data to a server via HTTP POST.</para> /// </summary> /// <param name="uri">The target URI to which form data will be transmitted.</param> /// <param name="formData">Form fields or files encapsulated in a WWWForm object, for formatting and transmission to the remote server.</param> /// <returns> /// <para>A UnityWebRequest configured to send form data to uri via POST.</para> /// </returns> public static UnityWebRequest Post(string uri, WWWForm formData) { UnityWebRequest unityWebRequest = new UnityWebRequest(uri, "POST"); unityWebRequest.uploadHandler = (UploadHandler) new UploadHandlerRaw(formData.data); unityWebRequest.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); using (Dictionary<string, string>.Enumerator enumerator = formData.headers.GetEnumerator()) { while (enumerator.MoveNext()) { KeyValuePair<string, string> current = enumerator.Current; unityWebRequest.SetRequestHeader(current.Key, current.Value); } } return unityWebRequest; }