我需要用Java(Java8)调用几个Web服务。第一个创建与某些元数据(autoGenerateDOI(...))关联的新标识符(DOI)。第二个针对该标识符(registerURL(...)注册一个URL。
第一个调用工作正常,但是当我尝试进行第二个调用时,我收到404错误作为响应。我试图创建一个独立程序来演示我在做什么(下)。谁能指出我做错了什么?
谢谢。
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class DOIdebugger {
public static String autoGenerateDOI(String doi, String creator, String title, String publisher, String publicationDate, String hostname){
String doiGenerated = "notYetCreated";
StringBuffer payload = new StringBuffer();
payload.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
payload.append("<resource xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://datacite.org/schema/kernel-4\" xsi:schemaLocation=\"http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.1/metadata.xsd\">");
payload.append("<identifier identifierType=\"DOI\"></identifier>");
payload.append("<creators>");
payload.append("<creator>");
payload.append("<creatorName>Creator Name</creatorName>");
payload.append("<nameIdentifier nameIdentifierScheme=\"ISNI\">Not available</nameIdentifier>");
payload.append("</creator>");
payload.append("</creators>");
payload.append("<titles>");
payload.append("<title>Test DOI creation</title>");
payload.append("<title titleType=\"Subtitle\">A DataShare</title>");
payload.append("</titles>");
payload.append("<publisher>abcxyz123</publisher");
payload.append("<publicationYear>2019</publicationYear>");
payload.append("<resourceType resourceTypeGeneral=\"Dataset\">Dataset</resourceType>");
payload.append("</resource>");
System.out.println("DatasetUtility::registerURL():: metadata payload=" + payload.toString());
try {
URL url = new URL("https://mds.test.datacite.org/metadata/10.17639");
HttpsURLConnection metaDataConnection;
metaDataConnection = initiateHttpsURLConnection(hostname, url, payload.toString().length());
System.out.println("metaDataConnection.getRequestMethod() = " + metaDataConnection.getRequestMethod());
OutputStreamWriter metaWriter = new OutputStreamWriter( metaDataConnection.getOutputStream() );
metaWriter.write(payload.toString());
metaWriter.flush();
metaWriter.close();
if(metaDataConnection.getResponseCode() == 200 || metaDataConnection.getResponseCode() == 201){
InputStreamReader reader = new InputStreamReader( metaDataConnection.getInputStream() );
StringBuilder response = new StringBuilder();
Scanner scanner = new Scanner(reader);
while(scanner.hasNext()) {
response.append(scanner.next());
}
scanner.close();
System.out.println( "\nResponse from server after POST:\n" + response );
doiGenerated = response.substring(3, (response.length()-1));
System.out.println( "\nDOI Generated:" + doiGenerated );
}
metaDataConnection.disconnect();
} catch (MalformedURLException malformedURLException) {
System.out.println("" + malformedURLException.getMessage());
} catch (IOException ioException) {
System.out.println("" + ioException.getMessage());
}
return doiGenerated;
}
public static String registerURL(String doi, String creator, String title, String publisher, String publicationDate, String hostname){
StringBuffer payload = new StringBuffer();
HttpURLConnection registerURLConnection = null;
try {
URL url = new URL("https://mds.test.datacite.org/doi/" + doi );
//construct payload
payload.append("doi=" + doi + "\nurl=https://www.example.com");
System.out.println("DatasetUtility::registerURL()::RegisterURL payload=\n*****\n" + payload.toString()
+ "\n*****\nlength=" + payload.toString().length());
registerURLConnection = initiateHttpsURLConnection(hostname, url, payload.toString().length());
System.out.println("DatasetUtility::registerURL():: RegisterURL::\nurl=" + registerURLConnection.getURL());
System.out.println("registerURLConnection.getRequestMethod() = " + registerURLConnection.getRequestMethod());
//send request
OutputStreamWriter registerURLWriter = new OutputStreamWriter( registerURLConnection.getOutputStream() );
registerURLWriter.write(payload.toString());
registerURLWriter.flush();
registerURLWriter.close();
int responseCode = registerURLConnection.getResponseCode();
System.out.println("DatasetUtility::registerURL():: RegisterURL:: connection response=" + responseCode);
if (responseCode!=200 || responseCode!=201){
System.out.println("Error on registering URL for DOI");
System.out.println("registerURLConnection msg= " + registerURLConnection.getResponseMessage());
Scanner scanner = new Scanner(registerURLConnection.getErrorStream());
while(scanner.hasNext()) {
System.out.println("ErrorStream: " + scanner.next());
}
scanner.close();
} else {
System.out.println("Successfully registered URL. Response code:" + responseCode);
}
} catch (MalformedURLException malformedURLException) {
System.out.println("MFE:" + malformedURLException.getMessage());
} catch (IOException ioException) {
System.out.println("IOE:" + ioException.getMessage());
}
return "Successful";
}
private static HttpsURLConnection initiateHttpsURLConnection(String hostname, URL url, int contentLength) throws IOException {
System.out.println("\nHostname: " + hostname + "\turl:" + url + "\tcontentLength:" + contentLength + "\n");
HttpsURLConnection httpsURLConnection = null;
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setRequestProperty("Authorization", "Basic " + edu.purdue.cybercenter.instance.actions.dataset.Base64.encodeToString("my_username:my_password".getBytes(), false) );
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
System.out.println("Set content length = " + contentLength);
return httpsURLConnection;
}
public static void main(String[] args){
String newDOI = autoGenerateDOI("doi", "abcxyz123", "Autogeneration of DOIs", "Creator Name", "2019", "www.example.com");
System.out.println("************************************************************************************");
registerURL(newDOI, "abcxyz123", "Autogeneration of DOIs", "Creator Name", "2019", "www.example.com");
}
}
示例输出为:
DatasetUtility::registerURL():: metadata payload=<?xml version="1.0" encoding="UTF-8"?><resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://datacite.org/schema/kernel-4" xsi:schemaLocation="http://datacite.org/schema/kernel-4 http://schema.datacite.org/meta/kernel-4.1/metadata.xsd"><identifier identifierType="DOI"></identifier><creators><creator><creatorName>Creator Name</creatorName><nameIdentifier nameIdentifierScheme="ISNI">Not available</nameIdentifier></creator></creators><titles><title>Test DOI creation</title><title titleType="Subtitle">A DataShare</title></titles><publisher>abcxyz123</publisher<publicationYear>2019</publicationYear><resourceType resourceTypeGeneral="Dataset">Dataset</resourceType></resource>
Hostname: www.example.com url:https://mds.test.datacite.org/metadata/10.17639 contentLength:701
Set content length = 701
metaDataConnection.getRequestMethod() = POST
Response from server after POST:
OK(10.17639/D4YJ-6075)
DOI Generated:10.17639/D4YJ-6075
************************************************************************************
DatasetUtility::registerURL()::RegisterURL payload=
*****
doi=10.17639/D4YJ-6075
url=https://www.example.com
*****
length=50
Hostname: www.example.com url:https://mds.test.datacite.org/doi/10.17639/D4YJ-6075 contentLength:50
Set content length = 50
DatasetUtility::registerURL():: RegisterURL::
url=https://mds.test.datacite.org/doi/10.17639/D4YJ-6075
registerURLConnection.getRequestMethod() = POST
DatasetUtility::registerURL():: RegisterURL:: connection response=404
Error on registering URL for DOI
registerURLConnection msg= Not Found
ErrorStream: Resource
ErrorStream: not
ErrorStream: found
但是,在命令行中使用具有相同目标URL和相同“有效载荷”的“ curl”调用Web服务是成功的。
curl -X PUT -H "Content-Type: text/plain;charset=UTF-8" --user my_username:my_password --data $'doi=10.17639/D4YJ-6075\nurl=http://example.org/' https://mds.test.datacite.org/doi/10.17639/D4YJ-6075
答案 0 :(得分:0)
就像莫里斯·佩里(Maurice Perry)的评论一样,我使用了错误的请求方法。