我正在使用 Microsoft 堆栈:Windows Server 2008 R2 / IIS 7.5;.NET 4.0
在本地主机上设置服务,客户端调用操作顺利。设置服务(最初作为 Post,然后作为 Get)我从 .NET 收到以下错误(方法如下)。如果我直接通过提琴手拨打电话,一切正常,(RAW)标头如下:
HTTP/1.1 200 OK 缓存控制:私有内容长度:19854411 内容类型:应用程序/xml;charset=utf-8 服务器:Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET
如果我通过控制台应用程序或 Windows 服务进行调用,我会收到以下错误“远程服务器返回错误:<411> 需要长度。”
我有一个 Web 服务,它应该在完成一些工作后返回一个 zip 文件
[OperationContract]
[WebInvoke(UriTemplate = "/GetFiles/{profileLocationId}/{batchSize}", Method = "GET", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
public byte[] ProfileImagesByProfileId(string profileLocationId, string batchSize)
{
// var watch = new Stopwatch();
// watch.Start();
try
{
long id = 0;
long.TryParse(profileLocationId, out id);
int size = 200;
int.TryParse(batchSize, out size);
var profileRoot = ConfigurationManager.AppSettings["ProfileImageRootDirectory"];
var fileType = ConfigurationManager.AppSettings["ProfileImageDefaultFileType"];
var packer = new ImagePackager(profileRoot, fileType);
if (profileLocationId != null)
{
var stream = packer.CompressImages(id, size);
return stream;
}
}
catch (Exception ex)
{
return new byte[0];
}
return new byte[0];
}
}
============================
客户端方法
static void TimerElapsed(object sender, ElapsedEventArgs e)
{
var id = GetLastProfileId();
// var uri = string.Format("http://{0}/TwitterService/ProfileImages/{1}/{2}", _ipAddress, id, _batchsize);
var uri = string.Format("http://{0}/Twitter_api/TwitterService/ProfileImages/{1}/{2}", _ipAddress, id, _batchsize);
GetProfileImagesFromService(uri);
}
private static void GetProfileImagesFromService(string uri)
{
try
{
Console.WriteLine("Process started: {0}", DateTime.Now.ToLongTimeString());
var request = WebRequest.Create(uri);
request.Method = "GET";
request.Timeout = 600000;
using (var response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(response.StatusDescription);
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream, Encoding.UTF8);
// var responseFromServer = reader.ReadToEnd();
var fileName = string.Format(@"{0}\{1:yyMMddhhmmss}.zip", _targetDirectory, DateTime.UtcNow);
// File.WriteAllText(fileName, responseFromServer);
// var xml = XDocument.Parse(responseFromServer);
var xml = XDocument.Load(reader);
var base64 = xml.Root.Value.ToCharArray();
var byteArray = Convert.FromBase64CharArray(base64, 0, base64.Length);
File.WriteAllBytes(fileName, byteArray);
Console.WriteLine("GetProfile cycle ended: {0}", DateTime.Now.ToLongTimeString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
任何指导将不胜感激。
先感谢您。