1 25 package com.scalagent.ksoap; 26 27 import java.io.*; 28 import javax.microedition.io.*; 29 30 import org.kxml.*; 31 import org.kxml.io.*; 32 import org.kxml.parser.*; 33 import org.kxml.wap.*; 34 35 public class HttpTransport { 36 37 String url; 38 String soapAction = "\"\""; 39 40 SoapEnvelope requestEnvelope = new SoapEnvelope(); 41 SoapEnvelope responseEnvelope = new SoapEnvelope(); 42 43 HttpConnection connection; 44 OutputStream os; 45 InputStream is; 46 InputStreamReader reader; 47 48 private boolean connected = false; 49 public String requestDump; 50 public String responseDump; 51 52 public HttpTransport(String url, String soapAction) { 53 this.url = url; 54 this.soapAction = soapAction; 55 } 56 57 public Object call(SoapObject obj) throws IOException, InterruptedException { 58 requestEnvelope.setBody(obj); 59 call(); 60 if (responseEnvelope.getBody() instanceof SoapFault) 61 throw((SoapFault)responseEnvelope.getBody()); 62 return responseEnvelope.getBody(); 63 } 64 65 public void call() throws IOException { 66 if (KSoapTracing.dbg) 67 KSoapTracing.log(KSoapTracing.DEBUG,"\n>> HttpTransport.call()"); 68 69 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 70 XmlWriter xw = new XmlWriter(new OutputStreamWriter (bos)); 71 requestEnvelope.write(xw); 72 xw.flush(); 73 bos.write('\r'); 74 bos.write('\n'); 75 byte [] requestData = bos.toString().getBytes(); 76 bos = null; 77 xw = null; 78 79 if (KSoapTracing.dbg) { 80 requestDump = new String (requestData); 81 KSoapTracing.log(KSoapTracing.DEBUG,"Request : " + requestDump); 82 } 83 84 try { 85 connected = true; 86 connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE,true); 87 connection.setRequestProperty("Content-Type","text/xml"); 88 connection.setRequestProperty("Content-Length",""+requestData.length); 89 connection.setRequestProperty("User-Agent","kSOAP/1.0"); 90 connection.setRequestProperty("Cookie",""); 91 92 connection.setRequestMethod(HttpConnection.POST); 93 94 os = connection.openOutputStream(); 95 os.write(requestData,0,requestData.length); 96 97 for (int i= 0; i<5; i++) { 98 try { 99 os.close (); 100 break; 101 } catch (Throwable e) { 102 } 104 } 105 106 requestData = null; 107 XmlParser xp = null; 108 109 is = connection.openInputStream(); 110 111 while (true) { 112 try { 113 if (KSoapTracing.dbg) { 114 bos = new ByteArrayOutputStream(); 115 byte [] buf = new byte [256]; 116 117 while (true) { 118 int rd = is.read(buf, 0, 256); 119 if (rd == -1) break; 120 bos.write(buf,0,rd); 121 } 122 buf = bos.toString().getBytes(); 123 responseDump = new String (buf); 124 KSoapTracing.log(KSoapTracing.DEBUG, 125 "HttpTransport.call() Reply : " + responseDump); 126 is.close(); 127 is = new ByteArrayInputStream(buf); 128 } 129 130 reader = new InputStreamReader(is); 131 xp = new XmlParser(reader); 132 responseEnvelope.read(xp); 133 134 if (KSoapTracing.dbg) 135 KSoapTracing.log(KSoapTracing.DEBUG, 136 "HttpTransport.call() responce = " + responseEnvelope.getBody()); 137 break; 138 } catch (Throwable e) { 139 if (e instanceof InterruptedIOException) { 140 } else { 142 KSoapTracing.log(KSoapTracing.ERROR,e.toString()); 143 if (e instanceof IOException) 144 throw (IOException)e; 145 } 146 } 147 } 148 149 if (KSoapTracing.dbg) 150 KSoapTracing.log(KSoapTracing.DEBUG, 151 "<< HttpTransport.call()"); 152 } finally { 153 if (KSoapTracing.dbg) 154 KSoapTracing.log(KSoapTracing.DEBUG, 155 "<< HttpTransport.call()" + connected +"\n\n"); 156 if (!connected) { 157 KSoapTracing.log(KSoapTracing.ERROR,"HttpTransport.call() EXCEPTION"); 158 throw new InterruptedIOException(); 159 } 160 reset(); 161 } 162 } 163 164 public void reset() { 165 connected = false; 166 if (reader != null) { 167 try { 168 reader.close(); 169 } catch (Throwable e) {} 170 reader = null; 171 } 172 if (is != null) { 173 try { 174 is.close(); 175 } catch (Throwable e) {} 176 is = null; 177 } 178 if (connection != null) { 179 try { 180 connection.close(); 181 } catch (Throwable e) {} 182 connection = null; 183 } 184 } 185 186 } 187 | Popular Tags |