C# - 通过WebRequest和HttpListener

时间:2017-09-21 08:41:31

标签: c# httprequest httplistener

我正在制作 ASP.NET Web应用程序,我用它来编辑Windows服务的设置(XML文件)。 Windows服务可以在与Web应用程序相同的服务器上运行,我已经得到了完美的工作,我遇到的问题是当Windows服务在不同的服务器上运行并且Web应用程序需要将Xml文件发送到Windows服务。

我的目标是让ASP.NET Web应用程序将刚刚通过Http更新的XML文件发送到具有HttpListener的Windows服务。

到目前为止我管理的内容

ASP.NET应用程序:

public string PushSettings(string serverName, string xmlFile)
{
    var prefix = "http://" + serverName+ ":";
    var portnumber = ConfigurationManager.AppSettings["PortNumber"];
    var command = ConfigurationManager.AppSettings["PushSettingsCommand"];
    var request = (HttpWebRequest)WebRequest.Create(prefix + portnumber + command);
    var bytes = System.Text.Encoding.ASCII.GetBytes(xmlFile);
    request.ContentType = "text/xml; encoding='utf-8'";
    request.ContentLength = bytes.Length;
    request.Method = "POST";
    var requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);
    requestStream.Close();

    try
    {
        var response = (HttpWebResponse)request.GetResponse();
        return response.StatusCode.ToString();
    }
    catch (WebException ex)
    {
        Log.Info("WebException thrown while trying to connect to httplistener. Exception message: " + ex.Message + " The following request was attempted: " + prefix + portnumber + command);
        throw new WebException("WebException thrown while trying to connect to httplistener. Exception message: " + ex.Message + " The following request was attempted: " + prefix + portnumber + command);
    }
}

string xmlFile 是整个XML文件的字符串)

Windows服务:

public void RunHttpListener()
{
    var listener = new HttpListener();
    try
    {
        listener.Prefixes.Add(_Prefix);

        listener.Start();

        var context = listener.GetContext();
        var request = context.Request;
        var ipAdress = context.Request.RemoteEndPoint?.ToString();

        if(_Prefix.Contains("PushSettings"))
        {
            SaveFile(context.Request.ContentEncoding, getSettings.GetBoundary(context.Request.ContentType), context.Request.InputStream);
        }

        var response = context.Response;
        const HttpStatusCode responseString = HttpStatusCode.OK;
        var buffer = Encoding.UTF8.GetBytes(responseString.ToString());
        response.ContentLength64 = buffer.Length;
        var output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        output.Close();
        listener.Stop();
    }
    catch (Exception ex)
    {
        Log.Info(ex.Message);
        if (listener.IsListening)
        {
            listener.Stop();
        }
    }
}

_Prefix 例如:{serverName} / PushSettings)

GetBoundary()和SaveFile()是我在这个Stack Overflow线程上找到的代码:Httplistener and file upload,它抛出异常,因为len == 0第三次循环通过第一次是(真){}。以下是该主题的代码:

public String GetBoundary(String ctype)
{
    return "--" + ctype.Split(';')[1].Split('=')[1];
}

public void SaveFile(Encoding enc, String boundary, Stream input)
{
    var boundaryBytes = enc.GetBytes(boundary);
    var boundaryLen = boundaryBytes.Length;

    using (var output = new FileStream("Settings.xml", FileMode.Create, FileAccess.Write))
    {
        var buffer = new Byte[1024];
        var len = input.Read(buffer, 0, 1024);
        var startPos = -1;

        // Find start boundary
        while (true)
        {
            if (len == 0)
            {
                throw new Exception("Start Boundaray Not Found");
            }

            startPos = IndexOf(buffer, len, boundaryBytes);
            if (startPos >= 0)
            {
                break;
            }
            else
            {
                Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
                len = input.Read(buffer, boundaryLen, 1024 - boundaryLen);
            }
        }

        // Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank)
        for (var i = 0; i < 4; i++)
        {
            while (true)
            {
                if (len == 0)
                {
                    throw new Exception("Preamble not Found.");
                }

                startPos = Array.IndexOf(buffer, enc.GetBytes("\n")[0], startPos);
                if (startPos >= 0)
                {
                    startPos++;
                    break;
                }
                else
                {
                    len = input.Read(buffer, 0, 1024);
                }
            }
        }

        Array.Copy(buffer, startPos, buffer, 0, len - startPos);
        len = len - startPos;

        while (true)
        {
            var endPos = IndexOf(buffer, len, boundaryBytes);
            if (endPos >= 0)
            {
                if (endPos > 0) output.Write(buffer, 0, endPos - 2);
                break;
            }
            else if (len <= boundaryLen)
            {
                throw new Exception("End Boundaray Not Found");
            }
            else
            {
                output.Write(buffer, 0, len - boundaryLen);
                Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
                len = input.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen;
            }
        }
    }
}

public Int32 IndexOf(Byte[] buffer, Int32 len, Byte[] boundaryBytes)
{
    for (var i = 0; i <= len - boundaryBytes.Length; i++)
    {
        var match = true;
        for (Int32 j = 0; j < boundaryBytes.Length && match; j++)
        {
            match = buffer[i + j] == boundaryBytes[j];
        }

        if (match)
        {
            return i;
        }
    }

    return -1;
}

这是一个(缩短的)XML文件的示例

<?xml version="1.0" encoding="UTF-8"?>
<MonitorSettings>
  <Interval>30000</Interval>
  <QueuCheck>False</QueuCheck>
  <MailAlert>False</MailAlert>
  <WindowsServices>mongodb</WindowsServices>
</MonitorSettings>

我到目前为止的Windows服务代码能够从Web应用程序接收请求,但它没有保存XML文件,因为当len == 0时抛出异常。有什么我做错了吗?有更好的办法吗?有什么好的StackOverflow线程可以提供帮助吗?

修改 更确切地说,异常在函数 SaveFile()中引发,第三次在while(true){}中循环:

if (len == 0)
{
    throw new Exception("Start Boundaray Not Found");
}

1 个答案:

答案 0 :(得分:0)

看起来我使用的是一种过于复杂的方法,试图将Request的主体保存到XML文件中。我做了一些研究,发现这个代码可以工作:

#app/config/config.yml
fos_rest:
    view:
        view_response_listener: 'force'