我要转换的 CURL 是
curl –insecure -H "Authorization: Bearer c25laGEyMDE0QHZ5b21sYWJzLmNvbTpwdW5lQDEyM2lSWWx6MFdtbjBxRjExcDVyQTE5eDRNNVk" -H "Content-Type: application/json" -d @Incident.txt https://na15.salesforce.com/services/apexrest/Incident/
我正在使用的java代码是这样做的
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
String url = "https://na15.salesforce.com/services/apexrest/Incident/";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
String userpass = "username" + ":" + "password+securitytoken_of_force.com";
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
System.out.println(basicAuth);
conn.setRequestProperty ("Authorization : Bearer", basicAuth);
String data = "{\"srd1\":\"HardwareIssue\"}";
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(data);
out.close();
new InputStreamReader(conn.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}
但是在执行代码时,我面临以下错误
“服务器返回 HTTP 响应代码:URL 的 401:https ://na15.salesforce.com/services/apexrest/Incident/ 。”
我是这样做的新手,请建议。