HttpWebResponse不适用于第一个请求,适用于后续请求

时间:2017-12-27 11:52:15

标签: c# wcf windows-services

我创建了一个WCF服务并将其托管为Windows服务。 从那个WCF服务,我试图从本地服务器获得响应。

HttpWebResponse 不适用于第一次(或有时是第二次)请求,但适用于后续请求。

这是我的代码:

public Hashtable getResponseFromReqtifyServer(String command)
{
    Hashtable httpResponse = new Hashtable();
    HttpWebResponse response = null;
    CookieContainer cookies = new CookieContainer();
    String html = string.Empty;
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(command, true)) as HttpWebRequest;                
        request.Method = "GET";                                                 
        request.CookieContainer = cookies;
        request.Proxy = null;
        request.Timeout = 10000000;
        response = (HttpWebResponse)request.GetResponse();                                

        if (response != null)
        {
            for (int i = 0; i < response.Headers.Count; i++)
            {
                string name = response.Headers.GetKey(i);
                if (name != "Set-Cookie")
                    continue;
                string[] values = response.Headers.Get(i).Split(';');

                foreach (string value in values)
                {
                    if (value.Contains("ReceiverId"))
                    {                                                                                                        
                        httpResponse.Add("Cookie", value);
                    }
                }
            }

            StreamReader reader = new StreamReader(response.GetResponseStream());                    
            html = reader.ReadToEnd();

            httpResponse.Add("result", html);
            httpResponse.Add("status", "OK");

            reader.Dispose();
            response.Dispose();                                           
        }
        else
        {
            httpResponse.Add("status", "ERROR");
            httpResponse.Add("error", "Response NULL!");
            httpResponse.Add("statusCode", HttpStatusCode.InternalServerError);
        }                
    }
    catch (WebException ex)
    {
        httpResponse.Add("status", "ERROR");
        httpResponse.Add("error", ex.Message);
        if(response == null)
            httpResponse.Add("statusCode", HttpStatusCode.InternalServerError);
        else 
            httpResponse.Add("statusCode", response.StatusCode);

        Console.WriteLine(ex);                               
    }               

    return httpResponse;
}

这是第一次给我以下错误:

  

“无法连接到远程服务器”

但对于后续请求,它不会给我这个错误。 此错误发生在以下行:

response = (HttpWebResponse)request.GetResponse();    

上面的代码有什么问题?

编辑:

以下是您的信息的更多详细信息。

我得到的例外类型是System.Net.WebException

例外情况:

  

System.Net.WebException:无法连接到远程服务器---&gt; System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:9091      在System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)      at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket&amp; socket,IPAddress&amp; address,ConnectSocketState state,IAsyncResult asyncResult,Exception&amp; exception)

堆栈追踪:

  

at System.Net.HttpWebRequest.GetResponse()      在F:\ Project \ ExternalServer \ Service \ ReqtifyServer.cs中的Reqtify.ReqtifyServer.getResponseFromReqtifyServer(String命令):第59行

0 个答案:

没有答案
相关问题