使用webrequest时如何保持连接活动?

时间:2011-03-08 08:49:35

标签: .net asp.net c webrequest

string strURL =(Request.IsSecureConnection?“https://website.com/Transaction.asmx”:“http://website.com/wTransaction.asmx”);

string strCommand = clsMisc.strGetDataFromPage(this,“c”,“”,PostMethod.BOTH); string strAppCode = clsMisc.strGetDataFromPage(this,“a”,“WEBIN”,PostMethod.BOTH); string strVenueCode = clsMisc.strGetDataFromPage(this,“v”,“”,PostMethod.BOTH); long transcationid = long.Parse(clsMisc.strGetDataFromPage(this,“t”,“0”,PostMethod.BOTH));                 string strParam1 = clsMisc.strGetDataFromPage(this,“p1”,“”,PostMethod.BOTH);                 string strParam2 = clsMisc.strGetDataFromPage(this,“p2”,“”,PostMethod.BOTH);                 string strParam3 = clsMisc.strGetDataFromPage(this,“p3”,“”,PostMethod.BOTH);                 string strParam4 = clsMisc.strGetDataFromPage(this,“p4”,“”,PostMethod.BOTH);

            StringBuilder sbrPost = new StringBuilder();

            string strIPAddress = Request.UserHostAddress.ToString();
            if (Request.Headers.ToString().Contains("&X-Forwarded-For="))
            {
                strIPAddress = Request.Headers["X-Forwarded-For"].ToString();
            }
            strAppCode = "|APPCODE=" + strAppCode + "|IPADDRESS=" + strIPAddress + "|";

            //strAppCode = "|APPCODE=" + strAppCode + "|IPADDRESS=" + Request.UserHostAddress.ToString() + "|";

            sbrPost.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sbrPost.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"><soap12:Body><objExecute xmlns=\"http://www.website.com/\">");
            sbrPost.Append("<strAppCode>" + strAppCode + "</strAppCode>");
            sbrPost.Append("<lngTransactionIdentifier>" + lngTransId + "</lngTransactionIdentifier>");
            sbrPost.Append("<strCommand>" + strCommand + "</strCommand>");
            sbrPost.Append("<strParam1>" + strParam1 + "</strParam1>");
            sbrPost.Append("<strParam2>" + strParam2 + "</strParam2>");
            sbrPost.Append("<strParam3>" + strParam3 + "</strParam3>");
            sbrPost.Append("<strParam4>" + strParam4 + "</strParam4>");
           </objExecute></soap12:Body></soap12:Envelope>");
            string strPost = sbrPost.ToString();

            WebRequest objReq = WebRequest.Create(strURL);
            WebResponse objRes;
            StreamReader smrRes;

            byte[] bytData = Encoding.UTF8.GetBytes(strPost);
            objReq.Method = "POST";
            objReq.ContentType = "application/soap+xml";
            objReq.ContentLength = bytData.Length;
            objReq.Timeout = 600000; // 10 minutes
            Stream objPost = objReq.GetRequestStream();
            objPost.Write(bytData, 0, bytData.Length);
            objPost.Close();
            objRes = objReq.GetResponse();
            smrRes = new StreamReader(objRes.GetResponseStream());
            Response.Write(smrRes.ReadToEnd());
            Response.ContentType = "text/xml";
            smrRes.Close();
            Response.End();
        }
        catch (Exception err)
        {
            clsLog.blnLogError(strErrorPage,strErrorMethod, "",err.Message);
        }
    }  

我在da line objPost.Close()上出现错误; .....这个异常错误是当我在visual studio 2010中使用F10慢慢地逐行调试这个代码时......代码工作正常...但是当我刚刚运行程序甚至快速调试程序时...它在该行引发错误..它给出了一个错误,即预期打开的连接被服务器关闭..

HELLLLPPP !!!

1 个答案:

答案 0 :(得分:0)

尝试Thread.Sleep(10000),如果您认为它在调试时工作正常。