Java – soap service using NTLM authentication

I tried to create an NTLM engine by following http://hc.apache.org/httpcomponents-client-4.3.x/ntlm.html I use NTLM authentication to use soap service, implement authschemefactory, and finally register authschemefactory with my HTTP client When I click on the service using my HTTP client, I get a response: "status code – 415, message – the server cannot service the request because the media type is not supported."

Anyone can tell me how to solve this unsupported media problem to use NTLM protected soap web services on the Java platform Using jcifs is a correct option to restrict NTLM protected services, or there is a better way Thank you in advance

DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getAuthSchemes().register(AuthSchemes.NTLM,new JCIFSNTLMSchemeFactory());

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    NTCredentials ntcred = new NTCredentials("USERNAME","PASSWORD","HOST","DOMAIN");

    credsProvider.setCredentials(new AuthScope("HOST",443,AuthScope.ANY_REALM,"NTLM"),ntcred);

    httpclient.setCredentialsProvider(credsProvider);

    httpclient.getParams().setParameter(
            CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");

    Writer writer = new StringWriter();
        writer.write("MY SOAP REQUEST BODY");

    HttpPost httppost = new HttpPost(
            "https://<HOST_NAME>/XiPay30WS.asmx");
    httppost.setEntity(new StringEntity(writer.toString()));

    httppost.setHeader("Content-Type","application/x-www-form-urlencoded");

    HttpResponse httpresponse = httpclient.execute(
                new HttpHost("HOST","https"),httppost,new BasicHttpContext());

    String statusCode = httpresponse.getStatusCode();

Solution

Use NTLM auth with flash to realize Python implementation

If you want to use Java, run the following separate flask code and call the URL from the Java code via HTTP request (e.g. post request / Dora / httpwithntlm)

from flask import  Flask,render_template,flash,request,url_for,redirect,session,Response
import requests,sys,json
from requests_ntlm import HttpNtlmAuth
app = Flask(__name__)




@app.route("/dora/httpWithNTLM",methods=['POST'])
def invokeHTTPReqWithNTLM():
    url =""
    reqData = json.loads(request.data)        
    reqxml=request.data
    headers = {}
    headers["SOAPAction"] = "";
    headers["Content-Type"] = "text/xml"
    headers["Accept"] = "text/xml"
    print("req headers "+str(request.headers))

    r = requests.Request("POST",url,auth=HttpNtlmAuth('domain\\username','password'),data=reqxml,headers=headers) 

    prepared = r.prepare()
    s = requests.Session()
    resp = s.send(prepared)
    print (resp.status_code)
    return Response(resp.text.replace("&lt;","<").replace("&gt;",">"),resp.status_code)




if __name__ == '__main__':
    app.run(host="0.0.0.0",port=5001)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>