Thursday, August 23, 2012

How to read the data from request URL and response stream value to request URL?

HttpWebRequest class sending the data using streamwriter and getting response from particular URL.


string strUrl="website";
HttpWebRequest request = WebRequest.Create(string.Format(strUrl, "valid")) as HttpWebRequest;
request.Method = strMethod;
            request.ContentType = "text/xml; charset=utf-8";
            request.ContentLength = PostData.Length;
            request.KeepAlive = false;
            writer = new StreamWriter(request.GetRequestStream());

            HttpWebResponse Response = (HttpWebResponse)request.GetResponse();
            Stream GetDataStream = Response.GetResponseStream();
            StreamReader GetDatafromStream = new StreamReader(GetDataStream);
            acknownledgementresp = GetDatafromStream.ReadToEnd();

In ASPX Page

         System.IO.Stream str;
        Int32 strLen, strRead;
        str = Request.InputStream;
        strLen = Convert.ToInt32(str.Length);
        byte[] strArr = new byte[strLen];
        strRead = str.Read(strArr, 0, strLen);
        string DataReceived = System.Text.Encoding.Default.GetString(strArr);

        Response.ContentType = "text";
        Response.BufferOutput = false;
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(ReceivedData);
        Response.OutputStream.Write(bytes, 0, bytes.Length);

Thursday, August 16, 2012

dynamically POST the FORM to third party URL / Site

 We want to POST FORM to third party URL or any other site. But in ASP.Net pages we cant do directly. so below customized function to send FORM dynamically.

private void ResponseToClient()
            NameValueCollection data = new NameValueCollection();
            data.Add("Field1", "val1");
            data.Add("Field2", "val2");
            string ResponseURL = "responsesimulator.aspx";

            string strForm = ConstructInternalFormToPost(ResponseURL, data);
            this.Page.Controls.Add(new LiteralControl(strForm));
        catch (Exception Ex)



    private static String ConstructInternalFormToPost(string url, NameValueCollection data)

        string tmpformID = "PostForm";
        StringBuilder strConstructForm = new StringBuilder();
        strConstructForm.Append("<form id=\"" + tmpformID + "\" name=\"" +
                       tmpformID + "\" action=\"" + url +
                       "\" method=\"POST\">");

        foreach (string key in data)
            strConstructForm.Append("<input type=\"hidden\" name=\"" + key +
                           "\" value=\"" + data[key] + "\">");

        StringBuilder ConstructScript = new StringBuilder();
        ConstructScript.Append("<script language='javascript'>");
        ConstructScript.Append("var v" + tmpformID + " = document." +
                         tmpformID + ";");
        ConstructScript.Append("v" + tmpformID + ".submit();");
        return strConstructForm.ToString() + ConstructScript.ToString();

Tuesday, August 14, 2012

Monday, August 23, 2010

Saturday, August 21, 2010

<form action="" id="cse-search-box">
    <input type="hidden" name="cx" value="partner-pub-3239228842410427:5ea2x3-35nv" />
    <input type="hidden" name="ie" value="ISO-8859-1" />
    <input type="text" name="q" size="31" />
    <input type="submit" name="sa" value="Search" />
<script type="text/javascript" src=";lang=en"></script>

Tuesday, August 17, 2010

HTTPException was unhandled by User Code?

HTTPException was unhandled by User Code?

Error executing child request for ~/page.aspx.

Ans: if page.aspx not exists in your current directory or renamed, so check the name.

Microsoft JScript runtime error?

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '

<!DOCTYPE html P'.

Ans: Server.Transfer or Response.write wont work in Ajax enabled pages.