开发者

How to keep connection alive when using webrequest?

开发者 https://www.devze.com 2023-02-15 16:58 出处:网络
string strURL = (Request.IsSecureConnection ? \"https://website.com/Transaction.asmx\" : \"http://website.com/wTransaction.asmx\");

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", P开发者_StackOverflow中文版ostMethod.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);
        }
    }  

I get an error on da line objPost.Close();.....the unusual error is that when I debug this code line by line slowly using F10 in visual studio 2010...the code works..but when I just run the program or even debug the program fast...it throws an error at that line.. it gives an error that the connection which was expected to be open was closed by the server..

HELLLLPPP!!!


give a try to Thread.Sleep(10000), if you think it is working fine when you debug it.

0

精彩评论

暂无评论...
验证码 换一张
取 消