在Java中设置Web服务的基本身份验证

时间:2018-07-05 08:14:48

标签: java wsimport

我有一组由wsimport工具导入的类,下面是命令

C:\Users\seethark\Desktop\temp>wsimport -keep -verbose -XadditionalHeaders http:
//1xxxx-ipaddressxxx.241:13080/SM/7/Sherpa.wsdl
    parsing WSDL...
    CloseSherpaRequest.java
CloseSherpaResponse.java
CreateSherpaRequest.java
CreateSherpaResponse.java
ObjectFactory.java
ResolveSherpaRequest.java
ResolveSherpaResponse.java
RetrieveSherpaKeysListRequest.java
RetrieveSherpaKeysListResponse.java
RetrieveSherpaListRequest.java
RetrieveSherpaListResponse.java
RetrieveSherpaRequest.java
RetrieveSherpaResponse.java
Sherpa.java
SherpaInstanceType.java
SherpaKeysType.java
SherpaModelType.java
Sherpa_Service.java
UpdateSherpaRequest.java
UpdateSherpaResponse.java
package-info.java
common\ArrayType.java
common\AttachmentType.java
common\AttachmentsType.java
common\Base64Type.java
common\BooleanType.java
common\ByteType.java
common\DateTimeType.java
common\DateType.java
common\DecimalType.java
common\DoubleType.java
common\DurationType.java
common\FloatType.java
common\IntType.java
common\LongType.java
common\MessageType.java
common\MessagesType.java
common\ObjectFactory.java
common\ShortType.java
common\StatusType.java
common\StringType.java
common\StructureType.java
common\TimeType.java
common\package-info.java
Base64Binary.java
HexBinary.java
ObjectFactory.java
package-info.java

问题是为hp服务管理器连接Web服务,因此我将wsdl用于ws导入。使用这些类,我编写了一小段代码来获取事件详细信息。下面是该代码

import com.hp.schemas.sm._7.*;
import com.hp.schemas.sm._7.common.*;
import com.hp.schemas.sm._7.common.ObjectFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.bind.*;
import javax.xml.namespace.QName;


import org.w3._2005._05.xmlmime.*;
public class retrieve_inc {

    public static void main(String[] args) throws MalformedURLException, JAXBException {
String WS_URL = "http://xxx.xx.29.241:13080/SM/7/Sherpa.wsdl"; //this is given wsdl 
        URL url = new URL(WS_URL);
        RetrieveSherpaRequest retrive_inc = new RetrieveSherpaRequest();
        RetrieveSherpaResponse retrieve_keys_response = new RetrieveSherpaResponse();
        RetrieveSherpaRequest retrive_incident_details_req = new RetrieveSherpaRequest();
        retrive_inc.setAttachmentInfo(true);
        retrive_inc.setAttachmentData(true);
        retrive_inc.setIgnoreEmptyElements(true);
        retrive_inc.setUpdatecounter(null);
        retrive_inc.setHandle(null);
        retrive_inc.setCount(null);
        retrive_inc.setStart(null);
        SherpaInstanceType inc_instance = new SherpaInstanceType();
        inc_instance.setQuery(null);
        inc_instance.setUniquequery(null);
        inc_instance.setRecordid(null);
        inc_instance.setUpdatecounter(null);



        DateTimeType start_time  = new DateTimeType();

        JAXBContext jc = JAXBContext.newInstance("com.hp.schemas.sm._7");

        //StringType incident_id = new StringType();
        //incident_id.setValue("IM67095763");

        ObjectFactory fact = new ObjectFactory();  
        JAXBElement<StringType> incident_id = new JAXBElement(new javax.xml.namespace.QName(com.hp.schemas.sm._7.common.StringType.class.getSimpleName()),com.hp.schemas.sm._7.common.StringType.class , "IM67095763");
        inc_instance.setIncidentID(incident_id);

        SherpaKeysType keys_samp = new SherpaKeysType();
        keys_samp.setUpdatecounter(null);
        SherpaModelType model_samp = new SherpaModelType();
        model_samp.setKeys(keys_samp);
        model_samp.setInstance(inc_instance);
        retrive_inc.setModel(model_samp);


        QName qname = new QName("http://130.29.29.241:13080/SM/7/", "RetrieveSherpaKeysListRequest"); //the end point i got from soap ui


        Sherpa_Service s_service = new Sherpa_Service();
        //Sherpa shp = (Sherpa) s_service.getPort(RetrieveSherpaResponse.class);

        s_service.create(qname);

        System.out.println("hello world");

        RetrieveSherpaResponse retrieve = s_service.getSherpa().retrieveSherpa(retrive_inc);

        }

}

我获得身份验证401错误,我知道有一个使用Web服务的用户名和密码,但是我不确定如何去设置它。如果可能的话,任何人都可以帮忙。

0 个答案:

没有答案