C #, Asp.net access API principles based on HTTP basic verification

Source: Internet
Author: User

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.