gpt4 book ai didi

java - 错误 :415 HttpURLConnection

转载 作者:行者123 更新时间:2023-11-30 04:04:49 27 4
gpt4 key购买 nike

我想连接到 web-service 并发送一个大文件,我使用 HttpURLConnection 如下:

 private void doFileUpload() {
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 8 * 1024 * 1024;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(new File(getRealPathFromURI(fileUri)));

URL url = new URL("https://url.com/service.asmx?op=Method");
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("SOAPAction", SOAP_ACTION);
conn.setRequestProperty("Host", "url.com");
conn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + lineEnd);
dos.writeBytes("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/>"
+ lineEnd);
dos.writeBytes("<soap:Body>" + lineEnd);
dos.writeBytes("<IncluirMultimedia xmlns=/" + "www.url.es/>/" + lineEnd);
dos.writeBytes("<identificadorGUID>" + Guid + "<" + "/identificadorGUID>" + lineEnd);
dos.writeBytes("<numeroServicio>" + codigoServicio + "<" + "/numeroServicio>" + lineEnd);
dos.writeBytes("<contenido>" + ficheroAEnviar + "<" + "/contenido>" + lineEnd);
dos.writeBytes("<tipoMultimedia>" + "0" + "<" + "/tipoMultimedia>" + lineEnd);
dos.writeBytes("<coordenadaLatitud>" + "0.0" + "<" + "/coordenadaLatitud>" + lineEnd);
dos.writeBytes("<coordenadaLongitud>" + "0.0" + "<" + "/coordenadaLongitud>" + lineEnd);
dos.writeBytes("<extension>" + "mp4" + "<" + "/extension>" + lineEnd);
dos.writeBytes("<cuando>" + "0" + "<" + "/cuando>" + lineEnd);
dos.writeBytes("<IncluirMultimedia>" + lineEnd);
dos.writeBytes("</soap:Body>" + lineEnd);
dos.writeBytes("</soap:Envelope>");
buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
dos.write(buffer, 0, bytesRead);
}
BufferedReader r = new BufferedReader(new InputStreamReader(fileInputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
fileInputStream.close();

dos.flush();
dos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

try {
conn.getContentLength();
if (conn.getResponseCode() >= 400) {
inStream = new DataInputStream(conn.getInputStream());
}
else {
inStream = new DataInputStream(conn.getErrorStream());
}
inStream.close();

}
catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
}

我的 soap-request 必须是:

POST /url/url.asmx HTTP/1.1
Host: url.es
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "www.url.com/IncluirMultimedia"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<IncluirMultimedia xmlns="www.url.es">
<identificadorGUID>string</identificadorGUID>
<numeroServicio>string</numeroServicio>
<contenido>base64Binary</contenido>
<tipoMultimedia>int</tipoMultimedia>
<coordenadaLatitud>string</coordenadaLatitud>
<coordenadaLongitud>string</coordenadaLongitud>
<extension>string</extension>
<cuando>int</cuando>
</IncluirMultimedia>
</soap:Body>
</soap:Envelope>

我不能使用 ksoap2,因为我需要发送一个非常大的文件,这会导致 OutOfMemoryError。这就是我需要使用此类的原因。

我收到错误 415,我做错了什么?

最佳答案

尝试使用:

 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

您正在发送 xml,我猜服务器需要它。

关于java - 错误 :415 HttpURLConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11883702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com