Java: http publish request
•
Java
I have to send a request to the web service to authenticate the user with a user name and password
I have questions about the following post requests:
public String postTest(String action,ConnectionParametrData [] parameters) {
Uri.Builder builder = new Uri.Builder().scheme(scheme).authority(authority).path(action);
uri = builder.build();
BufferedReader in = null;
String ans = null;
HttpPost request = new HttpPost(uri.toString());
HttpClient defaultClient = new DefaultHttpClient();
try {
request.setHeader("Content-Type","application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(getValuePairs(parameters)));
HttpResponse response = defaultClient.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"),8192);
StringBuffer sb = new StringBuffer("");
String line = "";
String newLine = System.getProperty("line.separator");
while((line = in.readLine()) != null) {
sb.append(line + newLine);
}
ans = sb.toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ans;
}
When I execute this method, the server throws an error telling me that the request is not a publish request
But this method is very effective:
private String makePost(String action,ConnectionParametrData [] parameters) throws IOException {
StringBuilder urlBuild = new StringBuilder();
urlBuild.append(scheme).append("://www.").append(authority).append(action);
URL url = new URL(urlBuild.toString());
URLConnection urlConnection = url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream());
String content = getParameters(parameters);
printout.writeBytes(content);
printout.flush();
printout.close();
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()),8192);
StringBuffer sb = new StringBuffer("");
String line = "";
String newLine = System.getProperty("line.separator");
while((line = in.readLine()) != null) {
sb.append(line + newLine);
}
in.close();
return sb.toString();
}
I prefer httpclient to urlconnection. Who knows why the first method is not approved as post?
Solution
In your first code snippet, I don't see where you set any post parameters for login and password
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair> {
new BasicNameValuePair("login","myusername"),new BasicNameValuePair("password","somepassword")};
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
You may want to see this: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
