728x90 AdSpace

  • Latest News

    [Tips] GetResponseStream Reading Response From URL using HTTP Web Request C#

    Hi All
        Today is my lucky days,
    If you have problem for your httpwebrequest and httpwebresponse, please view below tips, maybe can help you so much ^^

    This below function will be show you how to using http web request in C# with GET and POST method to get data on the server.

    GetResponseStream Reading Response From URL using HTTP Web Request C#








    You can download full source code from here

    Post Method:
    See the below code:



    static private string getResponeseString(string user, string pw)
    {
    var request = (HttpWebRequest)WebRequest.Create
    ("http://idol.talktv.vn/login/check");

    var postData = "act=frm&username=" + user;
    postData += "&password=" + pw;
    var data = Encoding.ASCII.GetBytes(postData);

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;

    using (var stream = request.GetRequestStream())
    {
    stream.Write(data, 0, data.Length);
    }

    var response = (HttpWebResponse)request.GetResponse();
    return new StreamReader(response.GetResponseStream()).ReadToEnd();
    }



    Get Method:
    You can have any below function for do it!

    The first function:


    public static XmlDocument getXMLDocumentFromXMLTemplate(string inURL)
    {
    //Declare an HTTP-specific implementation of the WebRequest class.
    HttpWebRequest myHttpWebRequest = null;

    //Declare an HTTP-specific implementation of the WebResponse class
    HttpWebResponse myHttpWebResponse = null;

    //Declare XMLResponse document
    XmlDocument myXMLDocument = null;

    //Declare XMLReader
    XmlTextReader myXMLReader = null;

    try
    {
    //Create Request
    myHttpWebRequest = (HttpWebRequest) HttpWebRequest.Create(inURL);
    myHttpWebRequest.Method = "GET";
    myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

    //Get Response
    myHttpWebResponse =
    (HttpWebResponse)myHttpWebRequest.GetResponse();

    // Process the data. Now load the XML Document
    myXMLDocument = new XmlDocument();

    //Load response stream into XMLReader
    myXMLReader = new XmlTextReader(
    myHttpWebResponse.GetResponseStream());
    myXMLDocument.Load(myXMLReader);
    }
    catch (Exception myException)
    {
    throw new Exception("Error Occurred in
    AuditAdapter.getXMLDocumentFromXMLTemplate()", myException);
    }
    finally
    {
    myHttpWebRequest = null;
    myHttpWebResponse = null;
    myXMLReader = null;
    }
    return myXMLDocument;
    }



    The second function:


    private bool getResponeseString(string user)
    {
    string sURL = "https://id.zing.vn/v2/uname-suggestion?username=" +
    user + "&cb=zmCore.js998139";

    WebRequest request;
    request = WebRequest.Create(sURL);

    WebProxy myProxy = new WebProxy("myproxy", 80);
    myProxy.BypassProxyOnLocal = true;

    Stream objStream;
    objStream = request.GetResponse().GetResponseStream();
    StreamReader objReader = new StreamReader(objStream);

    return objReader.ReadToEnd();
    }



    You can download full source code from here

    If you have any feedback or questions, please leave your comment we can discuss about it!

    Wishes you have more healthy and have a nice day!
    Regards!
    Zidane


    • Blogger Comments
    • Facebook Comments

    0 comments:

    Post a Comment

    Item Reviewed: [Tips] GetResponseStream Reading Response From URL using HTTP Web Request C# Rating: 5 Reviewed By: Unknown
    Scroll to Top