2011-12-18 7 views
0

webservice를 호출하는 android에 SoapBuilder 클래스가 있습니다. 이제는 이전 응용 프로그램에서 사용 했으므로 훌륭하게 작동했습니다. 이 새로운 응용 프로그램의 문제점은 실제 응답을 되 찾는 대신, 나는 어떤 종류의 날개 땡기 기호처럼 보이게됩니다. 내 webservice는 내 다른 응용 프로그램과 같은 .net webservice입니다. 내가 볼 수있는 유일한 차이점은 내가 이전 응용 프로그램에서 ssl을 사용하고 있었고 지금은 그렇지 않다는 것입니다. 그래서 소켓 소켓 소켓을 소켓 소켓으로 바꾸었다. 저는 여기에 (의 BufferedReader를) 읽고 있습니다 때 나는 이상한 문자를 얻을 내 코드실제 텍스트 대신 내 응답에 이상한 기호가 나타납니다.

if (contentLength > 0) { 
    int readBytes = 0; 
    int c = -1; 
    while ((c = in.read()) != -1) { 
     contentBuilder.append((char) c); 
     readBytes++; 
    } 
}   

StringBuilder contentBuilder = new StringBuilder(contentLength); 여기 SoapBuilder 내 전체 코드입니다.

public class SoapBuilder { 
    private final String TAG = "SOAPBUILDER"; 
    String Server = SQBApplication.SOAP_SERVER; 
    String WebServicePath = SQBApplication.SOAP_WEBSERVICEPATH; 
    String SoapAction = ""; 
    String MethodName = ""; 
    String XmlNamespace = SQBApplication.SOAP_XMLNAMESPACE; 
    Integer Port = SQBApplication.SOAP_PORT; 
    Integer BufferSize = 2048; 
    // boolean IsSSL = true; 
    private ArrayList<String> ParamNames = new ArrayList<String>(); 
    private ArrayList<String> ParamData = new ArrayList<String>(); 

    public SoapBuilder(String soapMethod) { 
     MethodName = soapMethod; 
     SoapAction = XmlNamespace + MethodName; 
    } 

    public SoapBuilder(String server, String soapMethod) 
    { 
     MethodName = soapMethod; 
     SoapAction = XmlNamespace + MethodName; 
     Server = server; 
    } 

    public SoapBuilder(String server, String webServicePath, String namespace, String soapMethod) 
    { 
     MethodName = soapMethod; 
     XmlNamespace = namespace; 
     SoapAction = XmlNamespace + MethodName; 
     Server = server; 
     WebServicePath = webServicePath; 
    } 

    public void AddParameter(String Name, String Data) { 
     ParamNames.add(Name); 
     ParamData.add(Data); 
    } 

    public String sendRequest() throws Exception { 
     String retval = ""; 
     String message = ""; 

     StringBuilder postAction = new StringBuilder(); 
     // send an HTTP request to the web service 
     postAction.append("POST " + WebServicePath + " HTTP/1.1\n"); 
     postAction.append("Content-Type: text/xml; charset=utf-8\n"); 
     postAction.append("SOAPAction: \"" + SoapAction + "\"\n"); 
     postAction.append("Host: " + Server + ":" + Port + "\n"); 
     postAction.append("Content-Length: %s\n"); 
     postAction.append("Expect: 100-continue\n"); 
     postAction.append("Accept-Encoding: gzip, deflate\n"); 
     postAction.append("Connection: Close\n"); 
     postAction.append("\n"); 
     ArrayList<String> envelope = new ArrayList<String>(); 
     envelope.add("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"); 
     envelope.add("<s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"); 
     envelope.add("<" + MethodName + " xmlns=\"" + XmlNamespace + "\">"); 
     // Parameters passed to the method are added here 
     for (int t = 0; t < ParamNames.size(); t++) { 
      String name = (String) ParamNames.get(t); 
      String data = (String) ParamData.get(t); 
      envelope.add("<" + name + ">" + data + "</" + name + ">"); 
     } 
     envelope.add("</" + MethodName + ">"); 
     envelope.add("</s:Body>"); 
     envelope.add("</s:Envelope>"); 

     int si = 0; 
     for (int i = 0; i < envelope.size(); i++) { 
      si += envelope.get(i).length(); 
     } 

     StringBuilder bodyBuilder = new StringBuilder(si); 
     for (int i = 0; i < envelope.size(); i++) { 
      bodyBuilder.append(envelope.get(i)); 
     } 
     message = String.format(postAction.toString(), 
     Integer.toString(bodyBuilder.length())); 
     message += bodyBuilder.toString(); 
     Socket socket = null; 
     boolean autoflush = true; 
     try { 
      SocketFactory socketFactory = (SocketFactory) SocketFactory 
       .getDefault(); 
      socket = (Socket) socketFactory.createSocket(Server, Port); 
      PrintWriter out = new PrintWriter(socket.getOutputStream(), 
      autoflush); 

      System.out.println("request length: " + message.length()); 
      if(message.length() > Integer.MAX_VALUE) 
      { 
       throw new IOException("message length exceeds max size"); 
      } 
      socket.setSendBufferSize(message.length()); 
      //only except if the message wouldn't have succeeded anyway 
      if(socket.getSendBufferSize() < message.length() && socket.getSendBufferSize() != message.length()) 
      { 
       throw new Exception(); 
      } 

      out.print(message); 
      out.flush(); 

     } catch (Exception e) { 

     } 

     BufferedReader in = new BufferedReader(new InputStreamReader(
     socket.getInputStream(), "UTF-8"), BufferSize); 
     StringBuilder response = new StringBuilder(); 

     int ci; 

     Pattern httpPattern = Pattern.compile(
      "HTTP/1.\\d\\s+(\\d+)\\s+[\\w\\s]+\\r\\n", 
      Pattern.CASE_INSENSITIVE); 
     Pattern contentLengthPattern = Pattern.compile(
      "Content-Length\\:\\s*(\\d+)\\r\\n", 
      Pattern.CASE_INSENSITIVE); 
     int contentLength = -1; 
     String httpResponse = ""; 
     while ((ci = in.read()) != -1) { 
      response.append((char) ci); 
      Matcher lengthMatcher = contentLengthPattern.matcher(response 
       .toString()); 
      Matcher httpMatcher = httpPattern.matcher(response.toString()); 

      if (lengthMatcher.find()) { 
       contentLength = Integer.parseInt(lengthMatcher.group(1)); 
      } 
      if (httpMatcher.find()) { 
       httpResponse = httpMatcher.group(1); 
      } 

      if (contentLength > 0) { 
       // contentLength+=2; 
       break; 
      } 
     } 

     StringBuilder contentBuilder = new StringBuilder(contentLength); 
     if (contentLength > 0) { 
      int readBytes = 0; 
      int c = -1; 
      while ((c = in.read()) != -1) { 
       contentBuilder.append((char) c); 
       readBytes++; 
      } 

     } 

     System.out.println(httpResponse); 
     if (httpResponse.equals("200")) { 

      Pattern responsePattern = Pattern.compile(
       "<soap:Envelope.*?>(.+)</soap:Envelope>", 
       Pattern.CASE_INSENSITIVE | Pattern.DOTALL 
       | Pattern.MULTILINE); 

      Pattern responseNullPattern = Pattern.compile("<" + MethodName 
       + "Response.+?/>", Pattern.CASE_INSENSITIVE 
       | Pattern.DOTALL | Pattern.MULTILINE); 
      Matcher responseMatcher = responsePattern 
       .matcher(contentBuilder); 
      Matcher responseNullMatcher = responseNullPattern 
       .matcher(contentBuilder); 
      System.out.println(contentBuilder); 
      if (responseMatcher.find()) { 
       retval = responseMatcher.group(0);// responseMatcher.group(0); 
      } else if (responseNullMatcher.find()) { 
       retval = null; 
      } 
     } else if (httpResponse.equals("500")) { 
      Pattern faultPattern = Pattern.compile(
       "<soap.Fault.+?>(.+)</soap.Fault>", 
       Pattern.CASE_INSENSITIVE | Pattern.DOTALL 
       | Pattern.MULTILINE); 
      Matcher faultMatcher = faultPattern.matcher(contentBuilder); 
      faultMatcher.find(); 
      retval = faultMatcher.group(0); 
     } else { 
      throw new Exception(String.format(
       "HTTP response not recognized: %s", httpResponse)); 
     } 

     in.close(); 
     socket.close(); 
     return retval; 
    } 
} 
+0

쇼, 잘못된 데이터 (16 진수가 아닌 문자) 기대와 함께 반환 : Wycats 당신이 읽어야하는이에 탁월한 게시물 (루비 특이성을 무시) 썼다. –

+0

많은 사람들이 닮았습니다. 주로 wingdings – user638049

+0

문자가 아닌 육각형 덤프를하십시오. –

답변

0

인코딩 문제가 발생하지 않을 수도 있습니다. 실제

http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/

+0

OK, 내 웹 서비스 정의는 요청이 UTF-8이 될 것으로 예상하고 위에서 볼 수 있듯이 내 soapbuilder는 UTF-8을 사용하므로 둘 다 동일합니다. – user638049

+0

그렇다고 체인의 모든 링크가 UTF-8 –

+0

을 사용하고 있다는 것을 의미하지는 않습니다. 그렇다면 왜 내 마지막 웹 서비스에서 제대로 작동합니까? 위의 soapbuilder를 기반으로 체인의 다른 부분을 점검해야합니까? – user638049

관련 문제