如何在 Android 中使用 WCF .SVC

2024-02-24

我红色了很多文档,但我无法使用这个 .svc 文件。 我对 .ASMX 文件没有任何问题。只有 SVC 我无法消费。真是非常非常非常压力......!我无法修改 WCF 部分。 我发布 WSDL 文件并尝试使用它:

WSDL

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
 xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
 xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
 xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" 
 xmlns:tns="http://tempuri.org/" 
 xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
 xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" 
 xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" 
 xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
 xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
 xmlns:wsa10="http://www.w3.org/2005/08/addressing" 
 xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" name="ValidateUser_wcf" 
 targetNamespace="http://tempuri.org/">
<wsp:Policy wsu:Id="WSHttpBinding_IValidateUser_wcf_policy">
<wsp:ExactlyOne>
<wsp:All>
<wsaw:UsingAddressing/>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://URL/WebServices/validateuser.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://URL/WebServices/validateuser.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IValidateUser_wcf_ValidateUser_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateUser"/>
</wsdl:message>
<wsdl:message name="IValidateUser_wcf_ValidateUser_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateUserResponse"/>
</wsdl:message>
<wsdl:portType name="IValidateUser_wcf">
<wsdl:operation name="ValidateUser">
<wsdl:input wsaw:Action="http://tempuri.org/IValidateUser_wcf/ValidateUser" message="tns:IValidateUser_wcf_ValidateUser_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IValidateUser_wcf/ValidateUserResponse" message="tns:IValidateUser_wcf_ValidateUser_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WSHttpBinding_IValidateUser_wcf" type="tns:IValidateUser_wcf">
<wsp:PolicyReference URI="#WSHttpBinding_IValidateUser_wcf_policy"/>
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ValidateUser">
<soap12:operation soapAction="http://tempuri.org/IValidateUser_wcf/ValidateUser" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ValidateUser_wcf">
<wsdl:port name="WSHttpBinding_IValidateUser_wcf" binding="tns:WSHttpBinding_IValidateUser_wcf">
<soap12:address location="http://URL/WebServices/validateuser.svc"/>
<wsa10:EndpointReference>
<wsa10:Address>
http://URL/WebServices/validateuser.svc
</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

我的 Java #1。只是我复制了 xml,它是在嗅探有关原始 .NET 客户端的 HTTP POST 流量后出现的,我必须使用 Microsoft .Net Monitor 应用程序在 java 中复制它:

private static final String NAMESPACE = "http://tempuri.org/";
private static final String NAMESPACE_INFOCAD = "http://xxxx.xxx/"; 

private static final String SOAP_ACTION_VALIDATION = "IValidateUser_wcf/ValidateUser";
private static final String VALIDATION_METHOD = "ValidateUser";
private final static String VALIDATION_URI = "http://URL/WebServices/validateuser.svc";

// only a1 node in the header i know, each other is unknown for me. I put them there only because i saw them in soap net client http post
String request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
        "<s:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\">"
        + "<s:Header>"
        //+ "<a:Action s:mustUnderstand=\"1\"> http://tempuri.org/IValidateUser_wcf/ValidateUser </a:Action>"
        + "<a1 xmlns=\"http://xxxx.xxx/\">"+testtoken+"</a1>"
        + "<a:MessageID> urn:uuid:c075fc4e-1e70-48f7-838b-9fa68e7aaad1 </a:MessageID>"
        + "<ActivityId CorrelationId=\"0d4e09eb-5d09-4617-a8e1-ec99415caab5\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">"
        + "25adfad8-ab5f-474a-afe3-56941d4ad408"
        + "</ActivityId>"
        + "<a:ReplyTo> http://www.w3.org/2005/08/addressing/anonymous </a:ReplyTo>"
        + "<a:To s:mustUnderstand=\"1\"> http://URL/WebServices/validateuser.svc </a:To>"
        + "</s:Header>"
        + "<s:Body><ValidateUser xmlns=\"http://tempuri.org/\"><username>"+username+"</username><password>"+password+"</password></ValidateUser></s:Body>"
        + "</s:Envelope>";

    String result = CallWebService(VALIDATION_URI, NAMESPACE+VALIDATION_METHOD, request);   



public String CallWebService(String url, String soapAction, String envelope) {  

    final DefaultHttpClient httpClient = new DefaultHttpClient();
    // request parameters
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 15000);
    // set parameter
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

    // POST the envelope
    HttpPost httppost = new HttpPost(url);
    // add headers
    httppost.setHeader("soapaction", soapAction);
    httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

    String responseString = "Nothingggg";
    try {

        // the entity holds the request
        HttpEntity entity = new StringEntity(envelope);
        httppost.setEntity(entity);

        // Response handler
        ResponseHandler<String> rh = new ResponseHandler<String>() {
            // invoked when client receives response
            public String handleResponse(HttpResponse response)
                    throws ClientProtocolException, IOException {

                // get response entity
                HttpEntity entity = response.getEntity();                   

                // read the response as byte array
                StringBuffer out = new StringBuffer();
                byte[] b = EntityUtils.toByteArray(entity);

                // write the response byte array to a string buffer
                out.append(new String(b, 0, b.length));
                return out.toString();
            }
        };

         responseString = httpClient.execute(httppost, rh);

    } catch (Exception e) {
        e.printStackTrace();
        Log.d("me","Exc : "+ e.toString()); 

    }

    // close the connection
    httpClient.getConnectionManager().shutdown();
    return responseString;
}

我的 Java #2,作者:Ksoap2。在这种情况下我得到了这个错误:

XmlPullParserException: unexpected type 
(position:END_DOCUMENT null@1:1 in 
java.io.InputStreamReader@blablabla)

但我将 xml 字符串请求放入验证 xml 应用程序中,这样就可以了:

public String validateUser_WCF(String username, String password){

     String enctoken = TOKEN;
     String testtoken = TestTOKEN;      

     SoapSerializationEnvelope envelope = null;
     SoapObject request = null;
     HttpTransportSE httpTransportSE = null;

     try {
         request = new SoapObject(NAMESPACE, VALIDATION_METHOD);
         request.addProperty("username", username);
         request.addProperty("password", password);

         envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet = true; 
         envelope.setOutputSoapObject(request);

         Element[] header = new Element[2];  
         header[0] = new Element().createElement(NAMESPACE, "To");
         header[0].setName("a1");                 
         header[0].addChild(Node.TEXT, VALIDATION_URI);

         header[1] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
         header[1].addChild(Node.TEXT, testtoken);

         envelope.headerOut = header;                                 

         httpTransportSE = new HttpTransportSE(VALIDATION_URI);

         httpTransportSE.debug = true;
         //httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

         httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

         String response = (String) envelope.getResponse();
         Log.i("RESPONSE", response);
         return response;  

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
             Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
             Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
        }

我真的真的希望有人能帮助我。我到达了一个死点......


我自己解决了(WCF绑定必须是basicHttpBinding,否则不起作用):

private static final String NAMESPACE = "http://tempuri.org/";
private static String URL="your url";

private static final String SOAP_ACTION_VALIDATION = "IValidateUser_wcf/ValidateUser";
private static final String VALIDATION_METHOD = "ValidateUser";

public boolean validateUser_WCF(String username, String password){

    SoapSerializationEnvelope envelope = null;
    SoapObject request = null;
    HttpTransportSE httpTransportSE = null;

    try {
        request = new SoapObject(NAMESPACE, VALIDATION_METHOD);
        request.addProperty("username", username);
        request.addProperty("password", password);

        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true; 
        envelope.setOutputSoapObject(request);

        //////////////////////////////                               
        // here you can add a HEADER element if you want
        Element[] header = new Element[1];  

        header[0] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
        header[0].addChild(Node.TEXT, "HeaderTextContent");

        envelope.headerOut = header;
        //////////////////////////////                               

        httpTransportSE = new HttpTransportSE(URL+VALIDATION_URI, 10*10000); // second parameter is timeout
        httpTransportSE.debug = true;
        httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

        // if response is a simple text result, you can call SoapPrimitive, if not, you have to call SoapObject result and navigate in response's tree like an xml file
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        //To get the data.
        String textResult = result.toString();
        Log.i("textResult", textResult); 

        return true;

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }finally{
                // here you can see in LOG what is request you send and what is response received
                Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
                Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
    }

    return false;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android 中使用 WCF .SVC 的相关文章

  • Onclick 不会在 CardView 上触发

    我有一个OnClickListener on a CardView 仅当我点击内容之外的区域 TextViews ImageViews 时 侦听器才起作用 我的内部也有一个线性布局CardView 我希望当我点击屏幕上的任意位置时它就能工作
  • 我应该保留远程数据库的本地副本吗?

    我正在开发一个应用程序 基本上允许人们创建 加入和管理其他人的群组 群组内的人也可以互相发送消息 我一直在想哪条路会更好 保留包含所有信息的远程数据库 包括发送给用户和从用户发送的消息 并让应用程序在每次需要信息时查询服务器 甚至是它以前见
  • .NET WebBrowser 控件可以使用 IE9 吗?

    我意识到这是一个早期版本并且不稳定 我不会梦想在任何其他项目中将默认的 Web 浏览器控件替换为 IE9 但在这种情况下 我特别需要 IE9 与其他版本进行比较 我想让 NET WebBrowser 控件使用 IE9 而不是机器上默认版本的
  • JAX-WS:有状态 WS 在独立进程中失败

    我在 Tomcat 上部署了一个有状态的 Web 服务 它由工厂服务和主要 API 服务组成 并且工作得很好 工厂服务将 W3CEndpointReference 返回到主 API 实例 客户端使用会话 现在 我尝试将相同的服务作为独立应用
  • 在另一个 Intent 中发送 Intent

    也许我的处理方式是错误的 但我想在我自己的应用程序中响应我的 Android AppWidget 的点击事件并启动一个 Activity 当时我设置了PendingIntent我还有一个Intent我想在点击时启动它 我的onStartCo
  • WCF 服务已超出最大数组长度配额 (16384)

    我有一个 wsf 服务和一个客户端应用程序 在尝试与客户端和服务进行通信时 我收到以下消息 格式化程序在尝试反序列化消息时引发异常 尝试反序列化参数时出错http tempuri org blob http tempuri org blob
  • MSI 和 EXE 安装程序有什么区别,我应该选择哪一个? [复制]

    这个问题在这里已经有答案了 可能的重复 msi 和 setup exe 文件之间有什么具体区别 https stackoverflow com questions 1789530 what are the specific differen
  • 如何在托管应用程序的框架代码中调试 System.ExecutionEngineException

    我有一个作为服务运行的应用程序 该应用程序有时会崩溃 该应用程序是用 vb net 编写的 并在新线程 一次一个 上运行其任务 我已经收集了故障转储并开始分析 从应用程序创建的日志文件的分析 转储分析支持它 看来 当没有任务正在运行并且应用
  • 如何自动下载 Google 签名的通用 APK 以在 Google Play 之外分发?

    我有使用 Play 应用签名的应用 我想将应用程序上传到华为应用程序库 Google 建议从捆绑资源管理器下载已签名的通用 APK 然后上传到 Google Play 之外的商店 如果您还在 Google Play 之外分发您的应用或计划
  • async wait 在调用异步方法时返回 Task> 而不是 List

    我正在尝试了解 async wait 的用法 并且研究了一些博客文章 现在我已经编写了一个测试代码 但它没有按照我期望的方式工作 我有一个返回列表的方法 private List
  • 更改弹出对话框的背景颜色

    我编写了显示弹出对话框的 android 代码 但我想将背景颜色从黑色更改为白色 然后更改文字颜色 这是对话框的代码 mPrefs PreferenceManager getDefaultSharedPreferences this Boo
  • 在Android中绘制圆角矩形

    我已经发现这个问题 https stackoverflow com questions 5618402 how to draw rounded rectangle in android ui解决方案是这段代码
  • setKeyListener 将覆盖 setInputType 并更改键盘

    大家好 我在两个设备之间遇到问题 在实践中使用InputType和KeyListener我正在操纵一个EditText让它从数字键盘接收逗号和数字 有关更多背景信息 请检查我之前的question https stackoverflow c
  • 如何让实体框架初始化新创建的实体上的集合?

    我正在尝试用一些测试数据来种子我的数据库IDatabaseIntialiser像这样 protected override void Seed BlogDataContext context
  • Listview里面只有一个Element

    您好 我正在尝试将列表视图放入列表视图中的列表视图中 唯一的问题是只有第一个列表视图正确显示所有元素 此后的每个列表视图仅包含一个元素 UPDATE 创建我自己的不可滚动列表视图解决了这个问题 https stackoverflow com
  • Android 等距游戏引擎 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 是否有任何现有的开源或商业解决方案
  • 哪些 .NET 编程语言有 CodeDom 提供程序?

    除了 C VB NET C 托管和 C CLI 和 F 之外 哪些 NET 编程语言有自己的 CodeDom 提供程序 我考虑过为 UnrealScript 制作一个 但我遇到了一些奇怪的问题 比如代码二进制运算符类型 http msdn
  • C/C++ 通过 Android NDK 在 JNI 中看不到 Java 方法

    我正在尝试从使用 NDK 构建的 C 类文件调用 Java 方法 它不断抛出常见的 未找到非静态方法 错误并导致整个 Android 应用程序崩溃 下面的代码片段 有些东西可能不需要 但我按原样保留它们 因为焦点 问题在于refreshJN
  • Eclipse:缺少 Java 构建路径

    我正在尝试使用 Eclipse 来使用适用于 Android 的 Google SDK 教程 我能够让前两个项目正常工作 但是当运行第三个 R java 时消失了 所以我放弃了 我根据练习 3 的解决方案集制作了一个全新的包 它充满了错误
  • RecyclerView 不调用 onCreateViewHolder 或 onBindView

    没有收到任何错误 所有数据似乎都有效 由于某种原因 没有调用与视图相关的方法 我已确定以下事项 getItemCount 是唯一被调用的适配器方法 并且返回一个正整数值 我知道这将是你们将要查看的区域 构造函数正在被调用 成员变量有效 Pa

随机推荐