Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c# - how to get custom message headers of wcf service in android

I am using this ->> link <<- all code for setting custom message headers in wcf but i am unable to get these headers inform ation in android. if anyone have any experices in android. how to get these type of custom message header values in android.

Like this code in C#

public class SimpleCustomHeaderService : ISimpleCustomHeaderService
{
    public string DoWork()
    {
        //Do Work
        //...

        //Capture Headers
        var userName = GetHeader("web-user", "ns");
        var webNodeId = GetHeader("web-node-id", "ns");
        var webSessionId = GetHeader("web-session-id", "ns");

        Debug.WriteLine("User: {0} / Node: {1} / Session: {2}", userName, webNodeId, webSessionId);
        var s = string.Format("HeaderInfo: {0}, {1}, {2}",
            userName,
            webNodeId,
            webSessionId);

        return s;
    }

    private static T GetHeader(string name, string ns)
    {
        return OperationContext.Current.IncomingMessageHeaders.FindHeader(name, ns) > -1
            ? OperationContext.Current.IncomingMessageHeaders.GetHeader(name, ns)
            : default(T);
    }
}

But i am doing these setting header information in android like this, but unable to get. What is the change needed for getting header information.

Method 1

 private static final String    NAMESPACE   = "http://tempuri.org/";
 Element[] header = new Element[1];
 header[0]=buildAuthHeader();
 envelope.headerOut = header;
 Log.i("header", "" + envelope.headerOut.toString());

    envelope.dotNet = false;
    envelope.bodyOut = request;
    envelope.setOutputSoapObject(request);
    int Timeout = 15 * 1000;
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,1000*60*2);
    Log.i("bodyout", "" + envelope.bodyOut.toString());
    Log.i("REQUEST", "" + request.toString());

    try{
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);

        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        returnString = response.toString();
        Log.i("RESPONSE==", response.toString());

private Element buildAuthHeader() {
        String n_s = "ns";
            String web_user_test="john";
    int node_id=1234554;
    //String gid= UUID.randomUUID().toString();
    //String xmlns_soap1="http://schemas.xmlsoap.org/wsdl/soap/";
    //String Namespace="http://www.w3.org/2005/08/addressing";
    //String xmlns_soap="http://schemas.microsoft.com/2003/10/Serialization/";
    Element h = new Element().createElement(NAMESPACE, "ns");
    Element first = new Element().createElement(NAMESPACE, "web-user");
    first.addChild(Node.TEXT,web_user_test);
    h.addChild(Node.ELEMENT, first);

    Element second = new Element().createElement(NAMESPACE, "web-node-id");
    second.addChild(Node.TEXT, String.valueOf(node_id));
    h.addChild(Node.ELEMENT, second);

    Element third = new Element().createElement(NAMESPACE, "web-session-id");
    third.addChild(Node.TEXT, String.valueOf(UUID.randomUUID()));
    h.addChild(Node.ELEMENT, third);
        return h;
    }

I have got Output: RESPONSE== HeaderInfo: , 0, 00000000-0000-0000-0000-000000000000

Method 2

I have also doing this alternative setting header:

            U= new User();
    U.setProperty(0, "horrorgoogle");
    U.setProperty(1,node_id);
    U.setProperty(2,UUID.randomUUID().toString());

    headerList.add(new HeaderProperty("web-user", U.getProperty(0).toString()));
    headerList.add(new HeaderProperty("web-node-id", U.getProperty(1).toString()));
    headerList.add(new HeaderProperty("web-session-id", U.getProperty(2).toString()));

   androidHttpTransport.call(SOAP_ACTION, envelope,headerList);

I have also get same error both method.

I have got Output: RESPONSE== HeaderInfo: , 0, 00000000-0000-0000-0000-000000000000

Would you please, find out where is error on my code. I am very glad , if you really help me for this my big problem.

Thank you so much reading my problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't think that you had set your header correctly. I dont see where you set the header[0]. Maybe you have to insert this line of code

envelope.headerOut = header;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...