1.
public class Apirequest
{
public static string commonurl = @ "http://192.168.2.186:8080/sale/";
public static string commonurl = @ "http://115.29.102.31:9011/sale/";
#region Generic API Requests
<summary>
Common API Requests
</summary>
<param name= "ServerURL" ></param>
<param name= "Paramjsonstr" ></param>
<returns></returns>
public static string Commonapiaccess (String ServerURL, byte[] bytearr)
{
String strresult = "";
Try
{
HttpWebRequest commonrequest = (HttpWebRequest) webrequest.create (ServerURL);
Commonrequest.method = "POST";
Commonrequest.contenttype = "Application/x-json";
byte[] Bytearr = Encoding.UTF8.GetBytes (param);
using (Stream Reqstream = Commonrequest.getrequeststream ())
{
Reqstream.write (Bytearr, 0, bytearr.length);
}
using (HttpWebResponse Httpwresp = (HttpWebResponse) commonrequest.getresponse ())
{
Try
{
Stream MyStream = Httpwresp.getresponsestream ();
StreamReader sr = new StreamReader (MyStream, Encoding.UTF8);
StringBuilder Strbuilder = new StringBuilder ();
while ( -1! = Sr. Peek ())
{
Strbuilder.append (Sr. ReadLine ());
}
strresult = Httputility.urldecode (strbuilder.tostring ());
}
catch (Exception exp)
{
strresult = "400";//"Error:" + exp. Message;
}
}
}
catch (Exception exp)
{
strresult = "500";//"Error:" + exp. Message;
}
return strresult;
}
#endregion
}
2. Invoking an instance
String json = apirequest.commonapiaccess (Apirequest.commonurl + "Icity.do?m=getcitybyprovinceid", Encoding.UTF8.GetBytes (Httputility.urlencode ("{\" code\ ": \" "+ comprovince. Selectedvalue.tostring () + "\"} ", Encoding.UTF8));
String msg = "1";
if (json! = "500")
{
Try
{
list<city> list = jsonconvert.deserializeobject<list<city>> (JSON);
Comcity. DataSource = list;
Comcity. DisplayMember = "name";
Comcity. ValueMember = "code";
}
catch (Exception ex)
{
msg = "400";
}
}
Else
{MessageBox.Show ("Connection to Server failed");}
C # fetching Java interfaces