Windows Service使用HTTP Web请求

编程入门 行业动态 更新时间:2024-10-28 20:27:22
本文介绍了Windows Service使用HTTP Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

 

大家好, 请问是否有人可以在这方面为我提供指导.我正在运行Windows服务,该服务实际上请求一个Web URL并发布 xml文件以获取响应.只要它使用asp环境或控制台应用程序运行,它就可以正常工作,但是当我使用Windows服务运行它时.坏了我添加了一个日志功能,以了解我的代码在Web服务中中断的时间.我发现只要Windows服务运行以下行,它就会中断.

Hi All,Please if some body can guide me in this regards.I am running a windows service which actually requests a web URL and post xml file to recive a response. As long as it runs using asp environment or console applicationit works fine but when i run it using windows service. It breaks. I have added a log functionthere to find out at what point my code is break in webservice.I have found that whenever the my windows service runs the following line, it breaks.

 

var httpWebRequest =( HttpWebRequest ) WebRequest .Create (Uri);

var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);

 

它给出的错误."配置系统无法初始化". 我在这里编写完整的代码供大家参考.请有人帮助我真的很紧急.在此先感谢您如果有人可以提供示例示例. 公开

The error it gives " Configuration system failed to initialize"I am writing my complete code here for you guys to see. Please if someone can help me,it is really very urgent. Many thanks in advancePlease if someone can provide sample example of it. public

静态 字符串 GetResponse( 字符串 @xml)

{

static string GetResponse(string @xml)

{

//**//Debug.WriteLine (xml);

//xml = xml.Replace(" ; \",string.Empty);

DoSomeFileWritingStuff(

DoSomeFileWritingStuff(

" InsideGetResponse" );

"InsideGetResponse");

 

var httpWebRequest =( HttpWebRequest ) WebRequest .Create (乌里); //当我运行Windows service 时MyCode在这里中断

DoSomeFileWritingStuff(

var httpWebRequest = (HttpWebRequest)WebRequest.Create(Uri);  //MyCode breaks here when i runwindows service

DoSomeFileWritingStuff(

"CreateURI" );

httpWebRequest.AllowAutoRedirect = AllowRedirect;

"CreateURI");

httpWebRequest.AllowAutoRedirect = AllowRedirect;

字节 [] requestBytes = 编码 .ASCII.GetBytes(@xml);

httpWebRequest.Method =方法;

Byte[] requestBytes = Encoding.ASCII.GetBytes(@xml);

httpWebRequest.Method = Method;

httpWebRequest.ContentType = ContentType;

httpWebRequest.ContentType = ContentType;

httpWebRequest.ContentLength = requestBytes.Length;

httpWebRequest.ContentLength = requestBytes.Length;

httpWebRequest.KeepAlive =

httpWebRequest.KeepAlive =

false ;

DoSomeFileWritingStuff(

false;

DoSomeFileWritingStuff(

开始流式处理API"

"start to stream API");

 

//向API请求流

尝试

{

使用 ( 流 requestStream = httpWebRequest.GetRequestStream())

{

using (Stream requestStream = httpWebRequest.GetRequestStream())

{

requestStream.Write(requestBytes,0,requestBytes.Length);

requestStream.Write(requestBytes, 0, requestBytes.Length);

}

}

捕获 ( WebException 我们)

{

catch (WebException we)

{

如果 (我们响应== 空 ) 返回 字符串 .Empty;

if (we.Response == null) return string.Empty;

 

扔 ;

}

throw;

}

StringBuilder 构建器= 新 StringBuilder ();

StringBuilder builder = new StringBuilder();

 

尝试

{

使用 ( var res =( HttpWebResponse )httpWebRequest .GetResponse())

{

using (var res = (HttpWebResponse)httpWebRequest.GetResponse())

{

使用 ( var sr = 新 StreamReader (res.GetResponseStream(), 编码 .ASCII))

{

using (var sr = new StreamReader(res.GetResponseStream(), Encoding.ASCII))

{

builder.Append(sr.ReadToEnd());

builder.Append(sr.ReadToEnd());

}

}

}

捕获 ( WebException webException)

{

catch (WebException webException)

{

//**//Debug.WriteLine (发生远程服务器错误:");

//**//Debug.WriteLine (webException.InnerException);

}

DoSomeFileWritingStuff(

DoSomeFileWritingStuff(

完成获取回复" );

"Finish Get response");

 

返回 builder.ToString();

}

return builder.ToString();

}

#endregion

#endregion

}

推荐答案

您的问题不太清楚..:)

Your question is not that much clear.. :)

无论如何,我想这会有所帮助.

Anyhow I guess this will help..

在Windows服务中,不能放置长代码.因为onStart方法必须在一段时间后返回操作系统.尝试在单独的线程中运行您的方法,然后在onStart方法中启动该线程.

In your windows service you cannot put long codes. Because the onStart method has to return to the OS after a while. Try running your method inside a separate thread and start the thread inside your onStart method.

更多推荐

Windows Service使用HTTP Web请求

本文发布于:2023-10-07 01:38:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Service   Windows   Web   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!