Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过HTTPListener的C#-HTTPProxy-写入时异常_C#_Http_Proxy_Httplistener - Fatal编程技术网

通过HTTPListener的C#-HTTPProxy-写入时异常

通过HTTPListener的C#-HTTPProxy-写入时异常,c#,http,proxy,httplistener,C#,Http,Proxy,Httplistener,我只是有点沮丧。我在C#中创建了一个httplistener,它模拟Webrequest并将结果返回给浏览器。如果我直接创建响应,它将按预期工作 作品: if (file.StartsWith("/applet.jnlp")) { String answer = Form1.self.generateJNLP(); byte[] buffer = Encoding.UTF8.GetBytes(answer); ctx.Response.StatusCode = (int)

我只是有点沮丧。我在C#中创建了一个httplistener,它模拟Webrequest并将结果返回给浏览器。如果我直接创建响应,它将按预期工作

作品:

if (file.StartsWith("/applet.jnlp"))
{
    String answer = Form1.self.generateJNLP();
    byte[] buffer = Encoding.UTF8.GetBytes(answer);
    ctx.Response.StatusCode = (int)HttpStatusCode.OK;
    ctx.Response.ContentLength64 = buffer.Length;
    ctx.Response.ContentType = "text/html";
    using (Stream s = ctx.Response.OutputStream)
         s.Write(buffer, 0, buffer.Length);
    return;
}
以下内容不起作用:

else
{
            TcpClient client = new TcpClient("example.com",80);
            String header = "GET "+file+"\r\n"
                          + "Cache-Control: no-cache\r\n"
                          + "Connection: keep-alive\r\n"
                          + "Accept: text/html, image/gif, imag/jpeg+; q=.2, */*; q=.2\r\n"
                          + "Accept-Encoding: gzip\r\n"
                          + "Host: example.com\r\n"
                          + "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0";
            byte[] buffer = Encoding.UTF8.GetBytes(header + "\r\n\r\n");
            client.GetStream().Write(buffer, 0, buffer.Length);
            StreamReader sr = new StreamReader(client.GetStream());
            String responsecode = sr.ReadLine();
            if (responsecode.Contains("200 OK"))
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.OK;
            }
            else
            {
                ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
                //return;

            }
            int len = 0;

            while (true)
            {
                String line = sr.ReadLine();
                if(line=="")
                    break;
                if (line.ToLower().StartsWith("content-length: "))
                {
                    String tmp = line.ToLower().Replace("content-length:","").Trim();
                    len = Int32.Parse(tmp);
                    continue;
                }
                try
                {
                    ctx.Response.Headers.Add(line);
                }
                catch (Exception e)
                {
                    MessageBox.Show("HEADER-ERROR: " + e.Message);
                }
            }
            ctx.Response.ContentLength64 = len;
            sr.ReadLine();
            buffer = new byte[len];
            int lastperc = -1;
            try
            {
                using(Stream s = (ctx.Response.OutputStream)) 
                {

                    for (int i = 0; i < len; i=i+4096)
                    {
                        try
                        {
                            byte[] tmp = new byte[4096];

                            int read = client.GetStream().Read(tmp, 0, 4096);
                            tmp.CopyTo(buffer, i);
                            int perc = 100000 / len * i;
                            if (perc > lastperc)
                                Console.WriteLine(((double)(perc / 100.0 * i)) + "%");
                            lastperc = perc;
                            s.Write(tmp, 0, read);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("READ: " + e.Message + "\r\n" + e.StackTrace);
                        }
                    }

               }
            }
            catch (Exception e)
            {
                Console.WriteLine("MYERROR: " + e.Message + "\r\n" + e.StackTrace);
                return;
            }

            File.WriteAllBytes(hashname, buffer);
            //client.Close();

        }
对不起,是德语,所以不知道例外情况的英文文本,由我翻译: 给定的网络名不再可用。
异常在s.write(…)处引发

它似乎在File.writealBytes()行上引发。此时,
hashname
是有效的文件路径吗?是的,它是正确的,他按照预期创建了文件。
Eine Ausnahme (erste Chance) des Typs "System.Net.HttpListenerException" ist in System.dll aufgetreten.
Der angegebene Netzwerkname ist nicht mehr verfügbar