Note that namespace is introduced
CoreCode:
using system. net;
using system. text;
using system. io; webrequest wR = webrequest. create (rssurl); // Where rssurl is the address of the API to be called
Wr. method = "Post"; specify the call method get post
Wr. contenttype = "application/X-WWW-form-urlencoded";
networkcredential NC = new networkcredential ("username", "password", "");
Wr. credentials = NC; // input the user name and password for httpbasic verification
string data = ""; // input the parameters to be provided to the API
stringb Uilder urlencoded = new stringbuilder ();
byte [] somebytes = NULL;
If (Data! = NULL)
{< br> asciiencoding encodeddata = new asciiencoding ();
somebytes = encodeddata. getbytes (data);
Wr. contentlength = somebytes. length;
stream newstream = Wr. getrequeststream ();
newstream. write (somebytes, 0, somebytes. length);
newstream. close ();
}< br> else
{< br> Wr. contentlength = 0;
}< br> string Re = "";
try
{< br> webresponse result = Wr. getresponse ();
stream receivestream = result. getresponsestream ();
Byte [] READ = new byte [512];
Int bytes = receivestream. Read (read, 0,512 );
Re = "";
While (Bytes> 0)
{
// Note:
// The following assumes that the response uses the UTF-8 as the encoding method.
// If the content is sent in the form of an ANSI code page (for example, 932), use a statement similar to the following:
// Encoding encode = system. Text. encoding. getencoding ("shift-JIS ");
Encoding encode = system. Text. encoding. getencoding ("gb2312 ");
Re + = encode. getstring (read, 0, bytes );
Bytes = receivestream. Read (read, 0,512 );
}
}
Catch (exception E)
{
Re = E. message;
}
Return re;