0

我正在使用ThingWorxExtensionPackageUploader通过 C# 上传扩展。当我从Visual Studio运行它时它可以工作,但是在将它作为 Windows 服务安装后,它似乎不再工作了。

public void UploadExtensionToThingWorx()
        {
            string endpoint = "/Thingworx/ExtensionPackageUploader";
            string url = "http://" + serverAddress + ":" + serverPort + endpoint;

            string filePath = Path.Combine(ExecutionDirectory, ExtensionFileName);
            byte[] fileContent = ReadFile(filePath);

            string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
            string contentType = "multipart/form-data; boundary=" + formDataBoundary;

            byte[] formData = GetMultipartFormData(formDataBoundary, ExtensionFileName, fileContent);

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = contentType;
            request.CookieContainer = new CookieContainer();
            request.ContentLength = formData.Length;
            request.Headers.Add("AppKey", applicationKey);
            request.Headers.Add("X-XSRF-TOKEN", "TWX-XSRF-TOKEN-VALUE");

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(formData, 0, formData.Length);
                requestStream.Close();
            }
        }

当我直接从 Visual Studio 运行代码时,代码正在工作。

4

0 回答 0