I am sending image and some text from my Android app to PHP server and I am using this code to do it:
...
URL url = new URL("http://example.com/upload.php");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("File", imagePath);
httpURLConnection.setRequestProperty ("Text", someText);
...
This works when someText variable is a word, but when it's a sentence with spaces between words it doesn't work.
Why is that? And is there a way to make it work?
Related
I would like to send a multipart request. Unfortunately the request fails, because the HttpURLConnection library inserts a number between the headers and the content, like this:
POST / HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data;boundary=*****
Transfer-Encoding: chunked
User-Agent: Dalvik/2.1.0 (Linux; U; Android 9; SM-G970F Build/PPR1.180610.011)
Host: localhost:7777
Accept-Encoding: gzip
115
test
Why is that number (115) there? How is it possible to avoid that?
This is my simplified code:
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://localhost:7777");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setChunkedStreamingMode(1024);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(conn.getOutputStream()));
dos.writeBytes("test");
fileInputStream.close();
dos.flush();
dos.close();
I am trying to pass parameters to a PHP Server using POST method in java but the response string I get is an error message on the line where the server tries to access option parameter at line:
Undefined index at line $option = $_POST["option"];
My code is as follows:
HttpURLConnection con = (HttpURLConnection)(new URL("http://--server-ip--")).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "option=1&type=1&coord=-33.867,151.206";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
con.connect();
Can anybody point out the mistake?
URL url = new URL("https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=mykey");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(bytearray);
wr.flush();
wr.close();
InputStream stream = conn.getInputStream(); //throws io exception
so basically I'm trying to send bytearray over httpurlconnection java.io.IOException: Error writing to server at last line.
I have some connection to url:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
How I can send post parameter to this url that php function: http://www.php.net/manual/en/reserved.variables.request.php can read it ? I try to according this question: Java - sending HTTP parameters via POST method easily but with no success. It just display empty array
php code:
print_r($_REQUEST,1)
java code:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("category_name", categoryName);
connection.setRequestProperty("complete", complete);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
connection.getResponseCode();
wr.flush();
wr.close();
connection.disconnect();
You forgot to set the Content-Type header, and you need to write the category_name and complete values as a string to the stream.
Quoting the code from the post you mentioned:
String urlParameters = "param1=a¶m2=b¶m3=c";
String request = "http://example.com/index.php";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();
As you can see, he sets the headers (Content-Type, Charset and Content-Length) and then writes the POST data to the stream.
The POST data is just like the GET format: key-value pairs seperated by &, and the key and value are seperated by =.
As the Content-Type is set to www-form-urlencoded, you need to 'url encode' the key and value values. You can do this using the URLEncoder.encode method.
The urlParameters for your POST data would be:
String urlParameters = "category_name=" + URLEncoder.encode(categoryName) + "&complete=" + URLEncoder.encode(complete);
I want to get content size by request.getContentLength() in server client JSP page.
But request.getContentLength() always return -1, i do not why?
Android snippet code:
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
//conn.setChunkedStreamingMode(100);
conn.setConnectTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setReadTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
//conn.setRequestProperty("content-length", "10");
//conn.addRequestProperty("content-length", "20");
conn.setFixedLengthStreamingMode(30);
conn.setRequestProperty("Charsert", ENCODING);
conn.setRequestProperty("Content-Type", "multipart/form-data"
+ ";boundary=" + java.util.UUID.randomUUID().toString());
conn.connect();
You are using conn.setChunkedStreamingMode(100) that will effectively enable the chunked transfer encoding in chunks of 100 bytes when the content-lenght is unknown in advance.
Use conn.setFixedLengthStreamingMode(int len) if you know in advance the length of the content you are going to send in the body of the request.