
正文
java代码post提交 java发送post请求 formdata
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 怎样发送 POST
private void postMethod(String url) throws IOException
{
url = "";
PostMethod postMethod = new PostMethod(url);
// 填入各个表单域java代码post提交的值
NameValuePair[] data = { new NameValuePair("id", "herrapfel"),new NameValuePair("passwd", "") };
// 将表单java代码post提交的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(" status code:" + statusCode);
// HttpClient对于要求接受后继服务java代码post提交的请求java代码post提交,象POST和PUT等不能自动处理转发
if(statusCode == HttpStatus.SC_OK)
{
StringBuffer contentBuffer = new StringBuffer();
InputStream in = postMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,postMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = reader.readLine()) != null)
{
contentBuffer.append(inputLine);
System.out.println("input line:"+ inputLine);
contentBuffer.append("/n");
}
in.close();
}
else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY)
{
// 从头中取出转向java代码post提交的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null)
{
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
}
else
{
System.err.println("Location field value is null.");
}
}
}
相关问答
Q1: java客户端通过http发送POST请求上传文件
这样的写法,是直接socket的做法。
如果是HTTP的,要按HTTP的协议进行。先了解一下multi-part的post
Q2: JAVA以POST方式提交XML获取返回值(返回格式为XML)
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setRequestMethod(“POST”);
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(xml);
outputStream.close();
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
Q3: 如何使用java发送post请求
/**
* 向指定 URL 发送POST方法java代码post提交的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数java代码post提交,请求参数应该是 name1=value1name2=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;
}
Q4: 如何在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){
ListNameValuePair nvps= new ArrayListNameValuePair();
nvps.add(new BasicNameValuePair("id", "1"));
String url="";
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,httpcore-4.1.4.jar和commons-logging-1.1.1.jar
Q5: java HttpPost怎么传递参数
这里有两种方法:
1、有时候考虑请求接口时,参数的形式是这样的:如"key1=value1key2=value2"
这样http get和post的方法都可以用同样的结构来作为参数,于是http post的方法请求服务器数据时可以用这样的方法来实现。
2、new BasicNameValuePair(keys[i], values[i]),这里写参数。
扩展资料:
httpPost其实在服务端模拟浏览器向其它接口发送服务的,一般情况下和httpclient,或者jsonp联合使用,可以把它理解为浏览器就行了,里面封装了http协议的一些东西,所以要对http协议有一定的了解。
超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信。HTTP 的工作方式是客户机与服务器之间的请求-应答协议。web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。
在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。
GET - 从指定的资源请求数据。POST - 向指定的资源提交要被处理的数据。
关于java代码post提交和java发送post请求 formdata的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







