在webrequest中上传字节数组时出错

时间:2018-06-16 08:47:01

标签: c# php

我正在尝试上传20mb的字节数组块,一切正常,直到我的程序到达块列表的近端,当我开始得到“底层连接已关闭:接收时发生意外错误。 “抛出为错误。该程序仅在尝试上载特定块时才会出现此错误;所有我知道这个块的不同或独特之处在于数组中第一个字节中的800个等于0,除了正确的标题是“008018012updates / Update.part176.zip ****** ******“作为字节数组。

我想知道为什么会这样,以及如何阻止它?

这是我的发送方法(C#):

    public static string SendBytes(string address, byte[] bytes) {
        for (int i = 0; i != 3; i++) {
            try { WebRequest request = WebRequest.Create(address);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded"; ;
                request.ContentLength = bytes.Length;
                Stream reqStream = request.GetRequestStream();
                reqStream.Write(bytes, 0, bytes.Length);
                reqStream.Close();
                StreamReader resStream = new StreamReader(request.GetResponse().GetResponseStream());
                string response = resStream.ReadToEnd();
                resStream.Close();
                return response;
            } catch (Exception e) { if (i == 2) { throw new Exception(e.Message); } }
            Thread.Sleep(3000);
        } return "ERROR: Couldn't send data.";
    }

这是我的接收代码(php):

<?php
    $config = "w.c";
    $request = file_get_contents('php://input');
    $dLeng = intval(substr($request, 0, 3));
    $fLeng = intval(substr($request, 3, 3));
    $pLeng = intval(substr($request, 6, 3));
    $dName = substr($request, 9, $dLeng);
    $fName = substr($request, 9 + $dLeng, $fLeng);
    $passW = substr($request, 9 + $dLeng + $fLeng, $pLeng);
    if (!Get_API_PW("./security.LOCK", $passW)) { echo "ERROR: Couldn't secure the folder."; return; }
    if (endsWith($fName, ".config") || $fName == $config) { echo "ERROR: Couldn't secure the folder."; return; }
    while (endsWith($dName, "//")) { $dName = substr($dName, 0, strlen($dName)-1); }
    if (!endsWith($dName, "/")) { $dName = $dName."/"; }
    while (strContains($dName, "..")) { $dName = str_replace("..", ".", $dName); }
    while (strContains($dName, " ")) { $dName = str_replace(" ", "", $dName); }
    while (substr($dName, 0, 1) == "/") { $dName = substr($dName, 1); }
    if (substr($dName, 0, 2) != "./") { $dName = "./".$dName; }
    if ($dName == "./") { echo "ERROR: Cannot write to root folder."; return; }
    while (strContains($fName, "/")) { $fName = substr($fName, 1); }
    if ($fName == "" || (!strContains($fName, ".") && !endsWith($fName, "."))) { echo "ERROR: Must have an extension."; return; }
    $data = substr($request, 9 + $fLeng + $dLeng + $pLeng);
    try { 
        if (!(file_exists($dName) || mkdir($dName, 0777, true))) { echo "ERROR: Folder access impossible."; return; }
        if (!(file_exists($config) && copy($config, $dName."web.config"))) { echo "ERROR: Couldn't secure the folder."; return; }
        $writer = fopen("./".$dName.$fName, 'w');
        fwrite($writer, $data);
        fclose($writer);
    } catch(Exception $E) { echo "ERROR: Could not properly write file data."; return; }
    echo "SUCCESS!";

    function endsWith($haystack, $needle) {
        $length = strlen($needle);
        return $length === 0 || (substr($haystack, -$length) === $needle);
    }

    function strContains($str, $req) {
        return (strpos($str, $req) !== false);
    }

    function Get_API_PW($sec, $guess) {
        try {
            $pReader = fopen($sec, "r");
            $pw = TDecode(fread($pReader, filesize($sec)));
            fclose($pReader);
            return $pw == $guess;
        } catch (exception $e) { return false; }
    }

    function TDecode($str) {
        if (!TEncoded($str)) { return $str; }
        return implode(array_map("chr", array_map('intval', str_split($str, 3))));
    }

    function TEncoded($str) {
        return (ctype_digit($str) && strlen($str) % 3 == 0);
    }
?>

0 个答案:

没有答案