Java projects and C # projects for communication

Source: Internet
Author: User

C # side code:

String contents = This.projectTypeNameTB.Text + "," + this.describeTB.Text;
Request
String uri = "Http://192.168.1.149:8080/zdys/TestServlte";//?action= "+ Httputility.urlencode (teststring);
HttpWebRequest request = Httpwebrequest.create (URI) as HttpWebRequest;
Request. Method = "POST";//Request Methods
Request. ProtocolVersion = new Version (1, 1);

string param = "This is the content that is routed to the Java side: Hello java! ";
string param = contents;
byte[] bs = Encoding.UTF8.GetBytes (param);
using (Stream Reqstream = Request. GetRequestStream ())
{
Reqstream.write (BS, 0, BS. Length);
Reqstream.close ();
}

HttpWebResponse response = Request. GetResponse () as HttpWebResponse;
using (Stream stream = response. GetResponseStream ())
{

StreamReader reader = new StreamReader (stream);
String responsestring = reader. ReadToEnd ();
Reader. Close ();
MessageBox.Show (responsestring);
}

Java-side code:

/**
* DoPost (use servlet to connect Elasticsearch ')
*/
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Using the Post method
Request.setcharacterencoding ("UTF-8");//Set character encoding
Response.setcontenttype ("Text/html;charset=utf-8");
Gets the stream at the same time that the encoding format is utf-8 (although the above has been set, but this one will have to be set again)
BufferedReader reader = new BufferedReader (New InputStreamReader (Request.getinputstream (), "Utf-8"));
String lines = Reader.readline ();//Get the C # side to transmit the data over the
System.out.println ("C # side transmits data is:" +lines);//lines = new String (Lines.getbytes (), "utf-8");
Reader.close ();//close connection
Addindexcontentserviceimpl AICSI = new Addindexcontentserviceimpl ();//new an implementation class that adds an indexed content interface
Projecttypeentity pte = new projecttypeentity (); New an implementation class that adds an indexed content object (to correspond to the data required on the C # side)
String[] array= stringtools.splitstring (lines, 2);//Custom Split the string to get the format you want
Pte.setprojecttype (Array[0]);
Pte.setdescrible (array[1]);
System.out.println (Pte.getdescrible () +pte.getprojecttype ());
String addcontent = Jsonfielddatatools.getprojecttypefielddata (PTE);//Call the appropriate method to assemble the string
Boolean flag=aicsi.addprojecttypecontent ("Zhongdi", "ProjectType", addcontent);//Perform the corresponding method of adding indexed content
if (flag) {
System.out.println ("add success");
}
Convert directly to the corresponding character under system default encoding, then output
PrintWriter out = Response.getwriter ();
Out.println ("This is the message returned to the C # end: Hello c#! ");
Out.flush ();
Out.close ();
String searchcontent = Urldecoder.decode (Request.getparameter ("Action"), "UTF-8");
Estools.createindex (index, indextype);//create indexes and mapping
Estools.close ();
System.out.println ("execution complete");
Response.sendredirect ("zdys/escontrol.jsp");

}

Java projects and C # projects for communication

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.