public class HttpURLConnectionPost {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
readContentFromPost()
}
public static void readContentFromPost() throws IOException {
URL postUrl = new URL("http://www.xxxxxxx.com")
// 打开连接
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection()
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true)
// Read from the connection. Default is true.
connection.setDoInput(true)
// 默认是 GET方式
connection.setRequestMethod("POST")
// Post 请求不能使用缓存
connection.setUseCaches(false)
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(true)
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded")
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect()
DataOutputStream out = new DataOutputStream(connection
.getOutputStream())
// 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
String content = "字段名=" + URLEncoder.encode("字符串值", "编码")
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写到流里面
out.writeBytes(content)
//流用完记得关
out.flush()
out.close()
//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))
String line
while ((line = reader.readLine()) != null){
System.out.println(line)
}
reader.close()
//该干的都干完了,记得把连接断了
connection.disconnect()
}
扩展资料:关于Java HttpURLConnection使用
public static String sendPostValidate(String serviceUrl, String postData, String userName, String password){
PrintWriter out = null
BufferedReader in = null
String result = ""
try {
log.info("POST接口地址:"+serviceUrl)
URL realUrl = new URL(serviceUrl)
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection()
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn
// 设置通用的请求属性
httpUrlConnection.setRequestProperty("accept","*/*")
httpUrlConnection.setRequestProperty("connection", "Keep-Alive")
httpUrlConnection.setRequestProperty("user-agent","Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1)")
httpUrlConnection.setRequestMethod("POST")
httpUrlConnection.setRequestProperty("Content-Type","application/jsoncharset=UTF-8")
Base64 base64 = new Base64()
String encoded = base64.encodeToString(new String(userName+ ":" +password).getBytes())
httpUrlConnection.setRequestProperty("Authorization", "Basic "+encoded)
// 发送POST请求必须设置如下两行
httpUrlConnection.setDoOutput(true)
httpUrlConnection.setDoInput(true)
// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(httpUrlConnection.getOutputStream(),"utf-8"))
// 发送请求参数
out.print(postData)
out.flush()
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(),"utf-8"))
String line
while ((line = in.readLine()) != null) {
result += line
}
//
// if (!"".equals(result)) {
// BASE64Decoder decoder = new BASE64Decoder()
// try {
// byte[] b = decoder.decodeBuffer(result)
// result = new String(b, "utf-8")
// } catch (Exception e) {
// e.printStackTrace()
// }
// }
return result
} catch (Exception e) {
log.info("调用异常",e)
throw new RuntimeException(e)
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close()
}
if(in!=null){
in.close()
}
}
catch(IOException e){
log.info("关闭流异常",e)
}
}
}
}
可以在命令行运行java程序时加上程序的参数。
例子如下:
public class HelloWorld{
public static void main(String args[]){
System.out.println("temp=" + args[0])
}
}
源代码截图:
命令行中截图:
解释一下: String[ ] args
该参数,是一个字符串数组,用来接收从命令行输入的参数
1. 参数之间用空格隔开
2. java解释器会自动处理,把用空格隔开的每一个参数通过args数组传递给买呢()方法。
3. 参数的下标从0开始,args[0]表示第一个参数,例如java HelloWorld 10 20 30
4.可以有多个空格,多个空格会被忽略。
扩展资料:
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等 。
参考资料:百度百科-网页链接
Java的方法参数传递都是值传递。这个问题需要楼主慢慢理解,如果基础薄弱(C、C++基础弱)就记住就好。如果基础好可以从内存角度理解。
从语法和简单层面理解:str是一个String类型的对象,传递给changeStr方法,传递的是str的引用内容,方法内的changeStr(String value),value变量和str指向了同一个对象,但是value重新赋值后,它的指向改变了,但是str的指向不会发生,所以str的输出还是“Hello”,而changeObjValue方法传入的TEST的对象pt给changeObjValue(TEST ref),ref变量和pt变量指向了同一对象,之后通过改变了对象的内容,pt变量的指向没变,但是执行的对象的变量变了。所以pt的ptValue值变了。
进一步理解,Java对字符串进行了特殊处理,因为字符串这种数据类型太常用了,为了节省内容提高效率。Java把字符串放入了常量池,所以我们没办法在运行时修改一个字符串对象的内容。也就是对value变量的赋值操作是重写建立了一个对象。楼主可以测试经典的java String的代码:
String a = "a";String b = "a"
String c = new String("a")
String d = new String("a")
System.out.println(a==b)
System.out.println(a==c)
System.out.println(b==c)
System.out.println(d==c)
再深入,Java没有C和C++的指针概念,可以粗暴的理解Java的引用是一种特殊的指针(实际上Java尽量把程序猿从指针中解放出来,或者说避免直接操作内存对象)。有一种不负责的说法:Java的基本类型传递传递的是值,也就是值的副本,引用类型传递的时引用。但是要注意String对象的内容不可变,而且传递的引用指向不可变。运行时Java的局部变量在栈空间中(线程私有),对象在堆内存中(线程公有),changeStr方法相当于在堆内存中又开辟了一块儿空间重写分配了一个对象,但是str没有指向这个新new出来的对象。而changeObjValue方法修改的pt指向的对象的内容也就是修改的是堆内存的数据,ref和pt指向的是一块儿内存。所以pt的对象数据会变化。