我有一些代码通过POST方法将一些字符串参数发送到服务器 我的代码是这样的:
public int uploadAghahi(aghahi AghahiToSend) {
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(AghahiToSend.imagePath);
if (!sourceFile.isFile()) {
prgDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
}
});
return 0;
} else {
try {
JSONObject params = new JSONObject();
params.put("mainsubjectid", "1");
params.put("subsubjectid", "22");
params.put("stateid", "144");
params.put("cityid", "144");
params.put("onvan", "سیاسیباسیباشیباشسیباسیبا");
params.put("address", "سیاسیباسیباشیباشسیباسیبا");
params.put("phone", "سیاسیباسیباشیباشسیباسیبا");
params.put("email", "hseify69@gmail.com");
params.put("tozihat", "سیاسیباسیباشیباشسیباسیبا");
FileInputStream fileInputStream = new FileInputStream(
sourceFile);
URL url = new URL(ADDRESSsendAghahi);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", AghahiToSend.imagePath);
// conn.setRequestProperty("content-type",
// "application/json;charset=UTF-8");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8"
+ lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"obj\""
+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(params.toString());
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
// Json_Encoder encode=new Json_Encoder();
// call to encode method and assigning response data to variable
// 'data'
// String data=encode.encod_to_json();
// response of encoded data
// System.out.println(data);
// Adding Parameter filepath
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"filepath\""
+ lineEnd);
// dos.writeBytes("Content-Type: text/plain; charset=UTF-8"
// + lineEnd);
// dos.writeBytes("Content-Length: " + name.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(AghahiToSend.imagePath); // mobile_no is String
// variable
dos.writeBytes(lineEnd);
// Adding Parameter media file(audio,video and image)
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
+ AghahiToSend.imagePath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
serverResponseMessage = conn.getResponseMessage();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String line = "";
while ((line = reader.readLine()) != null) {
result += line;
}
Toast.makeText(AddAghahiActivity.this, serverResponseMessage,
Toast.LENGTH_LONG).show();
if (serverResponseCode == 200) {
runOnUiThread(new Runnable() {
public void run() {
}
});
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
}
});
} catch (final Exception e) {
}
});
}
prgDialog.dismiss();
return serverResponseCode;
}
}
我有一个问题,它是编码字符串。 我的一些字符串是波斯语,当发送到服务器时,它们会改变其他字符,如下所示:
,3ED3ED3~'3E'3ED4
或
*G1'F
我如何正确发送params?
答案 0 :(得分:0)
为服务器找到适当的编码,并使用该编码对请求进行编码,并使用该编码对响应进行解码。例如,我昨天在Python上编写的代码:
params = urllib.parse.urlencode({'tool': tool, 'input': full_text, 'token': token}).encode("UTF-8") # Encoding parameters
result = urllib.request.urlopen(api_url, params) # Making request
readed_result = result.read().decode("UTF-8") # Decoding response
Java中的逻辑也一样。
如何为服务器找到合适的编码?检查http标头以进行编码。 例如: