How C # uses the rest interface to read and write data

Source: Internet
Author: User

Basic class, copy down the direct call on the line:

Using System;
Using System.IO;
Using System.Net;
Using System.Text;

public enum Httpverb
{
Get,//method commonly used on this kind of, of course you can also add other get: Get post: Modify put: Write Delete: delete
POST,
PUT,
DELETE
}

Namespace Httputils
{
public class Restclient
{
public string EndPoint {get; set;}//Requested URL address eg:http://215.23.12.45:8080/order/order_id=1&isdel=0
Public Httpverb method {get; set;}//Methods requested
public string ContentType {get; set;}//format type: I use Application/json, what to use, see the demand
public string PostData {get; set;}//Data transferred, of course I'm using a JSON string

Public Restclient ()
{
EndPoint = "";
Method = Httpverb.get;
ContentType = "Text/xml";
PostData = "";
}
Public restclient (String endpoint)
{
EndPoint = EndPoint;
Method = Httpverb.get;
ContentType = "Text/xml";
PostData = "";
}
Public Restclient (String endpoint, Httpverb method)
{
EndPoint = EndPoint;
Method = method;
ContentType = "Text/xml";
PostData = "";
}

Public Restclient (String endpoint, Httpverb method, String postdata)
{
EndPoint = EndPoint;
Method = method;
ContentType = "Text/xml";
PostData = PostData;
}


public string MakeRequest ()
{
Return MakeRequest ("");
}

public string makerequest (string parameters)
{
Uri uri = new Uri ("Http://localhost:8087/water-datacenter/dataTheme/historyData/historyData/getHisDataByIdsName?") ids=1&starttime=1989-12-31 00:00:00&endtime=2000-12-31 00:00:00&rtname=yyzcb&rtname=zfgxsf ");

var request = (HttpWebRequest) webrequest.create (URI);


var request = (HttpWebRequest) webrequest.create (EndPoint + parameters);

Request. Method = Method.tostring ();
Request. contentlength = 0;
Request. ContentType = ContentType;

if (!string. IsNullOrEmpty (postdata) && method = = httpverb.post)//If the transmitted data is not empty and the methods are POST
{
var encoding = new UTF8Encoding ();
var bytes = encoding.getencoding ("Iso-8859-1"). GetBytes (postdata);//encoding changes according to your own needs, I use in the project is UTF-8
Request. contentlength = bytes. Length;

using (var writestream = Request. GetRequestStream ())
{
Writestream.write (bytes, 0, bytes. Length);
}
}

if (!string. IsNullOrEmpty (postdata) && method = = httpverb.put)//If the transmitted data is not empty and the methods are PUT
{
var encoding = new UTF8Encoding ();
var bytes = encoding.getencoding ("Iso-8859-1"). GetBytes (postdata);//encoding changes according to your own needs, I use in the project is UTF-8
Request. contentlength = bytes. Length;

using (var writestream = Request. GetRequestStream ())
{
Writestream.write (bytes, 0, bytes. Length);
}
}
using (var response = (HttpWebResponse) request. GetResponse ())
{
var responsevalue = string. Empty;

if (response. StatusCode! = Httpstatuscode.ok)
{
var message = String.Format ("Request failed. Received HTTP {0} ", Response. StatusCode);
throw new ApplicationException (message);
}

Grab the response
using (var Responsestream = response. GetResponseStream ())
{
if (responsestream! = null)
using (var reader = new StreamReader (responsestream))
{
Responsevalue = reader. ReadToEnd ();
}
}

return responsevalue;
}
}

}//Class

}



The above is the class that demonstrates the method used:

/*
* Method 1
var client = new Restclient ();
String endPoint = @ "Http:\\myrestservice.com\api\";
var client = new Restclient (endPoint);
var json = client. MakeRequest ();
*/

var client = new Restclient ();
Client. EndPoint = @ "http:\\myrestservice.com\api\";;

Client. EndPoint = @ "Http://localhost:8087/water-datacenter/dataTheme/historyData/historyData/getHisDataByIdsName";;
http://localhost:8087/water-datacenter/datatheme/historydata/historydata/gethisdatabyidsname?ids=1& starttime=1989-12-31 00:00:00&endtime=2000-12-31 00:00:00&rtname=yyzcb&rtname=zfgxsf
Client. ContentType = "Application/json";
Client. Method = Httpverb.post;
Client. PostData = "{postdata:value}";
var json = client. MakeRequest ();
var json = client. MakeRequest ("? ids=1&starttime=1989-12-31 00:0:00&endtime=2000-12-31 00:00:00&rtname=yyzcb&rtname =zfgxsf "); With parameters

How C # uses the rest interface to read and write data

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.