我想连接到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
。这就是我需要使用这个课程的原因。
我得到error 415
,我做错了什么?
答案 0 :(得分:1)
尝试使用:
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
您正在发送xml,我想服务器会期望它。