java調用post
❶ java調用基於http+post+xml介面
1、直接用servlet就可以了,request.getInputStream(),然後解析xml,然後你的業務操作,組裝XML,response.getOutputStream()寫出去就OK了。
但這個性能低,而且還要依賴web容器。
2、socket實現http協議,把HTTP協議好好看看,自己解析(其實就是字元串的操作哦)。
3、你要是只做客戶端的話可以用httpClient,幾行代碼搞定了
❷ Java調用webservice和postmain調用的區別
區別是WebService可以有Get、Post、Soap、Document四種方式調用。
我們可以把webservice看做是web伺服器上的一個應用,web伺服器是webservice的一個容器。通過wximport生成代碼。通過客戶端編程方式。
通過URLConnection方式調用。
❸ 如何使用java模擬post請求
兩種選擇:一、使用httpclient,二使用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();
}
❹ java語言使用post方式調用webService方式
WebService可以有Get、Post、Soap、Document四種方式調用,以下Java通過post方式調用WebService代碼:
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.net.URL;
importjava.net.URLConnection;
importjava.net.URLEncoder;
importorg.apache.cxf.endpoint.Client;
importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
*功能描述:WebService調用
*
*/
publicclassClientTest{
/**
*功能描述:HTTP-POST
*
*/
publicStringpost(){
OutputStreamWriterout=null;
StringBuildersTotalString=newStringBuilder();
try{
URLurlTemp=newURL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity");
URLConnectionconnection=urlTemp.openConnection();
connection.setDoOutput(true);
out=newOutputStreamWriter(connection.getOutputStream(),"UTF-8");
StringBuffersb=newStringBuffer();
sb.append("byProvinceName=福建");
out.write(sb.toString());
out.flush();
StringsCurrentLine;
sCurrentLine="";
InputStreaml_urlStream;
l_urlStream=connection.getInputStream();//請求
BufferedReaderl_reader=newBufferedReader(newInputStreamReader(
l_urlStream));
while((sCurrentLine=l_reader.readLine())!=null){
sTotalString.append(sCurrentLine);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(null!=out){
try{
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnsTotalString.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);
// 打開和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,get,put請求
java中用post,get,put請求方法:
public static String javaHttpGet(String url,String charSet){
String resultData = null;
try {
URL pathUrl = new URL(url); //創建一個URL對象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); //打開一個HttpURLConnection連接
urlConnect.setConnectTimeout(30000); // 設置連接超時時間
urlConnect.connect();
if (urlConnect.getResponseCode() == 200) { //請求成功
resultData = readInputStream(urlConnect.getInputStream(), charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出錯!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("讀取數據流出錯!", e);
}
return resultData;
}
public static String javaHttpPost(String url,Map<String,Object> map,String charSet){
String resultData=null;
StringBuffer params = new StringBuffer();
try {
Iterator<Entry<String, Object>> ir = map.entrySet().iterator();
while (ir.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) ir.next();
params.append(URLEncoder.encode(entry.getKey(),charSet) + "=" + URLEncoder.encode(entry.getValue().toString(), charSet) + "&");
}
byte[] postData = params.deleteCharAt(params.length()).toString().getBytes();
URL pathUrl = new URL(url); //創建一個URL對象
HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();
urlConnect.setConnectTimeout(30000); // 設置連接超時時間
urlConnect.setDoOutput(true); //post請求必須設置允許輸出
urlConnect.setUseCaches(false); //post請求不能使用緩存
urlConnect.setRequestMethod("POST"); //設置post方式請求
urlConnect.setInstanceFollowRedirects(true);
urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset="+charSet);// 配置請求Content-Type
urlConnect.connect(); // 開始連接
DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 發送請求參數
dos.write(postData);
dos.flush();
dos.close();
if (urlConnect.getResponseCode() == 200) { //請求成功
resultData = readInputStream(urlConnect.getInputStream(),charSet);
}
} catch (MalformedURLException e) {
LogL.getInstance().getLog().error("URL出錯!", e);
} catch (IOException e) {
LogL.getInstance().getLog().error("讀取數據流出錯!", e);
} catch (Exception e) {
LogL.getInstance().getLog().error("POST出錯!", e);
}
return resultData;
}
❼ 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調用基於http+post+xml介面
1、直接用servlet就可以了,request.getInputStream(),然後解析xml,然後你的業務操作,組裝XML,response.getOutputStream()寫出去就OK了。
但這個性能低,而且還要依賴web容器。
2、socket實現http協議,把HTTP協議好好看看,自己解析(其實就是字元串的操作哦)。
3、你要是只做客戶端的話可以用httpClient,幾行代碼搞定了
❾ java調用post請求到localhost:4040
打包成jar就別用localhost了,改成伺服器的ip地址
❿ JAVA 中要調用httppost和HttpGet,在哪下怎麼導入
不知道你用的是哪個IDE,一般用的是Eclipse,當你使用post和get的時候,IDE會自動幫你導入相應類的。
應該是import javax.servlet.http.*;
doGet()和doPost()獲取HTML表單傳過來的數據。
這個是Java servlet中的內容