This below function will be show you how to using https web request in C# with GET and POST method to get data on the server.
πYou can download full source code from
Post Method:
See the below code:
static private string getResponeseString(string user, string pw)
{
var request = (HttpWebRequest)WebRequest.Create
("https://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
If you want to Add customer Header in HttpWebRequest, you can view example from here
How about topic GetResponseStream Reading Response From URL using HTTP Web Request C# . 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!
Webzone tech tips Zidane