java如何通過POST提交數據

javaME 如果終來端內存不源是很高的話 就不要用httpclient了
http://blog.csdn.net/iqueen/article/details/1820420 可以看一下 應該對你有幫助

Ⅱ 如何在java中發送post請求

package com.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class D {
public static void main(String[] args){
List<NameValuePair> nvps= new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("id", "1"));
String url="https://www.hao123.com/";

HttpClient httpClient = null;
String response="";
try {

HttpPost post = new HttpPost(url);
post.setHeader("Connection", "close");

httpClient = new DefaultHttpClient();
post.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse httpres= httpClient.execute(post);

if (httpres.getStatusLine().getStatusCode() >= 300) {
System.out.println("Request Failed,Code:" + httpres.getStatusLine().getStatusCode() + ",URL:" + url);
}

response = EntityUtils.toString(httpres.getEntity(), "utf-8");
}catch(Exception e){

e.printStackTrace();
}finally{
if(httpClient!=null){
httpClient.getConnectionManager().shutdown();
}

}
System.out.println(response);

}
}

需要httpclient-4.1.3.jar,-4.1.4.jar和commons-logging-1.1.1.jar

Ⅲ Java sendPost請求方法如何加入參數

/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

Ⅳ java 怎樣響應post請求

Http請求類

package wzh.Http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpRequest {
/**
* 向指定URL發送GET方法的請求
*
* @param url
* 發送請求的URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return URL 所代表遠程資源的響應結果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實際的連接
connection.connect();
// 獲取所有響應頭欄位
Map<String, List<String>> map = connection.getHeaderFields();
// 遍歷所有的響應頭欄位
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送GET請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}

/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}

調用方法:

public static void main(String[] args) {
//發送 GET 請求
String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456");
System.out.println(s);

//發送 POST 請求
String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
System.out.println(sr);
}

Ⅳ 怎麼使用java模擬post請求

你要導入httpclient的jar包,要是你請求參數格式是json的或者返回的是json格式數據,你還需要導入內json包
/**
* post請求
* @容param url url地址
* @param jsonParam 參數
* @param noNeedResponse 不需要返回結果
* @return
*/
public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
//post請求返回結果
DefaultHttpClient httpClient = new DefaultHttpClient();
JSONObject jsonResult = null;

Ⅵ java post請求參數怎麼寫

//serverURL url地址
HttpPost httpPost = new HttpPost(serverURL);
//param 為參數
StringEntity entity = new StringEntity(param);
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);

還可以用map作為參數
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
if(param!=null){
Set set = param.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = param.get(key);
formparams.add(new BasicNameValuePair(key.toString(), value.toString()));
}
}

Ⅶ java怎麼發送post請求參數

/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

Ⅷ 如何使用java發送post請求

/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}

Ⅸ 如何使用java模擬post請求

兩種選擇:一、使用,二使用java自帶的類庫。
1、java自帶類庫:

public static String call(String address,String params) {

URL url = null;

HttpURLConnection httpurlconnection = null;

StringBuilder result = new StringBuilder();

try {

url = new URL(address);

// 以post方式請求

httpurlconnection = (HttpURLConnection) url.openConnection();

httpurlconnection.setDoOutput(true);

httpurlconnection.setRequestMethod("POST");

if(null!=params&¶ms.length()>0){

httpurlconnection.getOutputStream().write(params.getBytes());

httpurlconnection.getOutputStream().flush();

httpurlconnection.getOutputStream().close();

}

// 獲取頁面內容

java.io.InputStream in = httpurlconnection.getInputStream();

java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, Config.DEFAULT_CHARSET));

String str = breader.readLine();

while (str != null) {

result.append(str);

str = breader.readLine();

}

breader.close();

in.close();

} catch (Exception e) {

} finally {

if (httpurlconnection != null)

httpurlconnection.disconnect();

}

return result.toString().trim();

}

2、httpclient:

public static String post(String url,String params){
HttpClient httpClient = new DefaultHttpClient();

StringBuilder builder = new StringBuilder();

HttpPost post = new HttpPost(url);

try {

if(null!=params){

post.setEntity(new StringEntity(params,"UTF-8"));

}

HttpResponse resp = httpClient.execute(post);

int statusCode = resp.getStatusLine().getStatusCode();

if(statusCode<=304){

HttpEntity entity = resp.getEntity();

if (entity == null) {

throw new IllegalArgumentException("HTTP entity may not be null");

}

if (entity.getContentLength() > Integer.MAX_VALUE) {

throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");

}

int i = (int)entity.getContentLength();

i = i<0 ? 4096 : i;

final InputStream instream = entity.getContent();

final Reader reader = new InputStreamReader(instream, Config.DEFAULT_CHARSET);

final CharArrayBuffer buffer = new CharArrayBuffer(i);

final char[] tmp = new char[1024];

int l;

while((l = reader.read(tmp)) != -1) {

buffer.append(tmp, 0, l);

}

builder.append(buffer);

}

post.abort();

} catch (Exception e) {

post.abort();

}

return builder.toString().trim();

}