Tuesday, February 11, 2014

When sending HttpGet request, URL contains spaces how to do?


            
To do a project, you need to upload the user's message to the server.
I'm using HttpGet request, the message content directly spliced ​​in the url above.
When will throw However, when the intermediate message content appears blank, execution HttpGet get = new HttpGet (url) statement exception: IllegalArgumentException
Great God ask you this how do ah?
Reply:
Their top!
Reply:
It seems unlikely that url parameter spaces Spaces can be used to handle the specified parameters URLEncoder.encode
Reply:
decode ah
Reply:
 HttpGet get = new HttpGet (URLEncoder.encode (url, "UTF-8")); 

Reply:
reference to the second floor sasuke38 reply:
url seems unlikely that the parameter spaces Spaces can be used to handle the specified parameters URLEncoder.encode


The how Encode it? Is the entire url into them yet? I just tried it, something like this:
(To maintain secrecy, I put the URL replaced baidu)
 
String url = "http://baidu.com?name=han&content=hello world";
HttpGet get = new HttpGet (URLEncoder.encode (url, "UTF-8"));

Then reported the anomaly:
java.lang.IllegalStateException:. Target host must not be null, or set in parameters scheme = null, host = null, path = http://baidu.com?name=han&content=hello world

Is not the whole url should not give Encode, ah? If only Encode behind the request, then the statement, and then how and in front of http://baidu.com fight together?



Reply:
URLEncoder.encode (). This method of all non-alphanumeric characters changed to% sequence (in addition to spaces, underscores, hyphens, dots and asterisks), if the entire url to encode it, =, & these characters will be encoded . So the best part of the coding-by.
 
String query = URLEncode.encode ("name");
query + = "=";
query + = URLEncode.encode ("han");
query + = "&";
query + = URLEncode.encode ("content");
query + = "=";
query + = URLEncode.encode ("hello world");

String url = "? Http://baidu.com" + query;

Reply:
I say deal with the meaning of the text specified parameters can be used to understand the best processing parameters is only possible space processing parameters
Reply:
reference to the 7th floor sasuke38 reply:
I say that is the meaning of the text processing specified parameters can be used to understand the best deal only with processing parameters possible parameter space

For example http://baidu.com?name=han&content=hello world then this treatment
url = "http://baidu.com?name=han&content =" + URLEncoder.encode ("hello world", "utf-8");
Reply:
Why not post? get the url length is limited, can not take many words
Reply:
Above have made it very clear.
Reply:
reference to the 7th floor sasuke38 reply:
I say that is the meaning of the text processing specified parameters can be used to understand the best deal only with processing parameters possible parameter space


Ah, I see, that's a little bit complicated to write ~ ~ Thank you!
Reply:
reference to the 9th floor woshilisao reply:
Why not post? get the url length is limited, can not take many words


php provided is get
Reply:
8th Floor, positive solution to solve my problem ..... praise.

No comments:

Post a Comment