Troubleshoot Java access. netwebservice FAQ

Source: Internet
Author: User

Not long before the company, wrote a Java call. NET write WebService results during the test with a variety of methods are not completed, always throw an exception, and finally directly using the SOAP message to make the call succeeded, the specific code is as follows, for reference only:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Querydatautil {
public static string Getopdetailstring (String regoinid,string regoinname,string signalname) throws exception{
String xml = "";
Xml= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>";
XML + = "<soapenv:envelope xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:soap=\"/HTTP// Www.w3.org/2003/05/soap-envelope\ "xmlns:tem=\" http://tempuri.org/\ "xmlns:soap-enc=\" http://schemas.xmlsoap.org /soap/encoding/\ "xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "xmlns:xsd=\" http://www.w3.org/2001/ Xmlschema\ ">";
XML + = "<soapenv:Header/>";
XML + = "<soapenv:Body>";
XML + = "<tem:QueryDeviceRealData><tem:regionId>" +regoinid+ "</tem:regionid><tem:regionname > "+regoinname+" </tem:regionName><tem:signalName> "+signalname+" </tem:signalname></tem: Querydevicerealdata> ";
String xml1= "</soapenv:Body>";
xml1+= "</soapenv:Envelope>";
XML +=XML1;
String urlstring= "Http://IP: Port/xxxx?wsdl";
HttpURLConnection httpconn = null;
OutputStream out = null;
String returnxml= "";
Set up batch Numbers
DateFormat df = new SimpleDateFormat ("Yyyymmddhhmmss");
String s= Df.format (New Date ());
System.out.println ("Start");
Httpconn = (httpurlconnection) new URL (urlstring). OpenConnection ();
Httpconn.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 ");
Httpconn.setrequestproperty ("SOAPAction", "xxx");
Httpconn.setrequestmethod ("POST");
Httpconn.setdooutput (TRUE);
Httpconn.setdoinput (TRUE);
Httpconn.connect ();
out = Httpconn.getoutputstream (); Get output Stream Object
Httpconn.getoutputstream (). Write (Xml.getbytes ()); The SOAP request character stream that will commit the server is written to the output stream
Out.flush ();
Out.close ();
int code = Httpconn.getresponsecode (); Used to get the server response status
String tempstring = null;
StringBuffer sb1 = new StringBuffer ();
if (code = = HTTPURLCONNECTION.HTTP_OK) {
BufferedReader reader = new BufferedReader (New InputStreamReader (Httpconn.getinputstream (), "UTF-8"));
while ((tempstring = Reader.readline ()) = null) {
Sb1.append (tempstring);
}
if (null! = Reader) {
Reader.close ();
}
} else {
BufferedReader reader = new BufferedReader (New InputStreamReader (Httpconn.geterrorstream (), "UTF-8"));
Read one line at a time until NULL is read to the end of the file
while ((tempstring = Reader.readline ()) = null) {
Sb1.append (tempstring);
}
if (null! = Reader) {
Reader.close ();
}
}
Response message
Returnxml=sb1.tostring ();
System.out.println ("returnopdetail===" +returnxml);
return returnxml;
}
}
I hope you have a lot of suggestions, huh?

Troubleshoot Java access. netwebservice FAQ

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.