java调用自定义参数的webservice,并要求传递用户名和密码

前段时间因为项目需要要调用一个webservice,这个webservice要求传递的参数是一个自定义类型的,第一次用webservice费了很大的力气才搞定。

请求的事例:

POST /PublicService/ASMX/WebSiteService.asmx HTTP/1.1
Host: www.xcszcg.com
Content-Type: text/xml; charset=utf-8
Content-Length: lengthSOAPAction: "http://topevery.org/InsertWXCSReport"



  
    
   
   
      string
      string
    
  
  
    
      
    
    
      dateTime
      string
      string
      string
      string
      string
      boolean
      string
      unsignedByte
      guid
      guid
      guid
    
  

返回事例:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length



 
   
     
   

   
      string
   

 


厂家接口描述:

public InsertResult InsertSiteReport(AcceptParameter entity);

函数描述

上报数字化城管案件。

实体类字段可以参考上面的请求和相应可以得知。

 

在这个过程中实验了三种方法:

第一种就是到网上找,但是总是提示无法实例化注册的类,弄了很久也没有弄好,于是放弃了。(在网上可以找到的)

第二步,就是利用给出的wsdl用ecplise文件本地化,但是本地化后项目并没有传递用户名和密码的方法,可能是我水平有限吧,最后放弃。(步骤网上有)

第三种,最后被逼无奈了,只好后用最原始的http请求了,先把请求的xml格式用字符串拼起来,然后发送过去,返回的xml有用sax解析,最后决绝乱码问题,最终搞定。

下面附上我的代码,共大家参考。

 

 

public class InsertWXCSReportService extends DefaultHandler{    

private InsertResult result = null;

 private String tagName;  

public static void postData(Reader data, URL endpoint, Writer output) throws Exception{        

HttpURLConnection urlc = null;     

try      {     

urlc = (HttpURLConnection) endpoint.openConnection();     

try      {     

urlc.setRequestMethod("POST");     

} catch (ProtocolException e)      {     

throw new Exception("Shouldn’t happen: HttpURLConnection doesn’t support POST", e);     

}     

urlc.setDoOutput(true);     

urlc.setDoInput(true);     
urlc.setUseCaches(false);     

urlc.setAllowUserInteraction(false);     

urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");         

OutputStream out = urlc.getOutputStream();         

try      {     

Writer writer = new OutputStreamWriter(out, "UTF-8");     

pipe(data, writer);     

writer.close();     

} catch (IOException e)      {     

throw new Exception("IOException while posting data", e);     

} finally {     

if (out != null)     

out.close();     

}         

InputStream in = urlc.getInputStream();     

try      {     

Reader reader = new InputStreamReader(in);     

pipe(reader, output);     

reader.close();     

} catch (IOException e)   {     

throw new Exception("IOException while reading response", e);     

} finally      {     

if (in != null)     

in.close();     

}         

} catch (IOException e)      {     

throw new Exception("Connection error (is server running at " + endpoint + "): " + e);     

} finally      {     

if (urlc != null)     

urlc.disconnect();     

}      }          

private static void pipe(Reader reader, Writer writer) throws IOException      {     

char[] buf = new char[1024];     

int read = 0;     

while ((read = reader.read(buf)) >= 0)      {     

writer.write(buf, 0, read);     

}     

writer.flush();     

}             

public InsertResult getInsertResult(AcceptParameter acceptParameter, InsertWXCSReportService dataCityService){    

//这部分是拼字符串      

String reqContent= "admin"         +"admin"         +""         +"" +         ""+acceptParameter.getDbCreateDate()+"" +         ""+acceptParameter.getPosition()+"" +         ""+acceptParameter.getDesc()+"" +         "

"+acceptParameter.getSummary()+"" +         ""+acceptParameter.getReporter()+"" +         ""+acceptParameter.getTelNum()+"" +         ""+acceptParameter.getIsReceipt()+"" +         ""+acceptParameter.getReplyWay()+"" +         ""+255+"" +         "";     

System.out.println(".....xml....req.."+reqContent);   

 try {   

 Reader reader = new InputStreamReader(new ByteArrayInputStream(reqContent.getBytes()));    

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();   

 Writer out = new OutputStreamWriter(byteOut);   

 //DataCtiyService poster = new DataCtiyService();    

this.postData(reader, new URL("http://www.xxxxxxx.xxxxxxx/ASMX/WebSiteService.asmx"), out);   

 System.out.println("............string...."+byteOut.toString());   

 if(byteOut.toString().length()>0){     

SAXParser parser = null;     

parser = SAXParserFactory.newInstance().newSAXParser();           

//DataCityService dataCityService = new DataCityService();           

InputStream xmlInputRes = new ByteArrayInputStream(byteOut.toString().getBytes());           

parser.parse(xmlInputRes, dataCityService);           

System.out.println(".......返回的结果......"+this.result.getAcceptNo());   

 }   } catch (Exception e) {

   // TODO Auto-generated catch block    e.printStackTrace();   }   

 return result;    }

 

 @Override  

public void characters(char[] ch, int start, int length)    throws SAXException {  

 if(this.tagName!=null){    

String data = new String(ch,start,length);       

 if("IsSuccess".equals(this.tagName)){     

System.out.println("....isSuccess......"+data);     

this.result.setIsSuccess(data+"");    

}    

if("AcceptNo".equals(this.tagName)){     

System.out.println("....AcceptNo......"+data);    

 this.result.setAcceptNo(data);    

}   

if("ErrorMsg".equals(this.tagName)){     

System.out.println("....ErrorMsg......"+data);     this.result.setErrorMsg(data);    }   }  }

 

 @Override  

public void endElement(String uri, String localName, String qName)    throws SAXException {   

// TODO Auto-generated method stub   

super.endElement(uri, localName, qName);

 }

 

 @Override  

public void startElement(String uri, String localName, String qName,    Attributes attributes) throws SAXException {   

if(qName.equals("types:InsertResult")){             

this.result = new InsertResult();       

}       

this.tagName=qName;

 }    

public InsertResult getResult() {

  return result;  

}  

public void setResult(InsertResult result) {   this.result = result;  }  

public String getTagName() {   return tagName;  }  

public void setTagName(String tagName) {   this.tagName = tagName;  }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/fumingxing/archive/2012/11/01/2750359.html

你可能感兴趣的:(java调用自定义参数的webservice,并要求传递用户名和密码)