当前位置: 技术问答>java相关
给分给分!一个小问题
来源: 互联网 发布时间:2015-06-02
本文导语: 我写了一个用POST方法向网页送QueryString的小程序!但不知道怎么样都写不上去!! 源码如下: package postwriter; import java.net.*; import java.io.*; class QueryString{ String query; QueryString(Object name,Object value){ query ...
我写了一个用POST方法向网页送QueryString的小程序!但不知道怎么样都写不上去!!
源码如下:
package postwriter;
import java.net.*;
import java.io.*;
class QueryString{
String query;
QueryString(Object name,Object value){
query = URLEncoder.encode(name.toString()) + "=" + URLEncoder.encode(value.toString());
}
QueryString(){
query = "";
}
public synchronized void add(Object name,Object value){
if(!query.trim().equals("")) query += "&";
query += URLEncoder.encode(name.toString()) + "=" + URLEncoder.encode(value.toString());
}
public String getString(){
return query;
}
}
class FormPoster{
private URL url;
private QueryString query = new QueryString();
FormPoster(URL url) throws IllegalArgumentException{
if(!url.getProtocol().toLowerCase().startsWith("http")){
throw new IllegalArgumentException("Posting only works for http URLs");
}
this.url = url;
}
void add(String name,String value){
query.add(name,value);
}
URL getURL(){
return this.url;
}
InputStream post() throws IOException{
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(),"ASCII");
out.write(query.getString());
out.write("rn");
out.flush();
out.close();
return uc.getInputStream();
}
}
public class Main {
public static void main(String[] args) {
URL url;
try{
url = new URL("http://be-ride.acz.jp/bbs");
}catch(MalformedURLException e){
System.out.println(e);
return;
}
FormPoster poster = new FormPoster(url);
poster.add("na","DF");
poster.add("mail","abc@abc.com");
poster.add("msg","test");
try{
InputStream in = poster.post();
InputStreamReader r = new InputStreamReader(in);
int ch;
while((ch=r.read()) != -1){
System.out.print((char) ch);
}
System.out.println();
in.close();
}catch(IOException e){
System.err.println(e);
}
}
}
源码如下:
package postwriter;
import java.net.*;
import java.io.*;
class QueryString{
String query;
QueryString(Object name,Object value){
query = URLEncoder.encode(name.toString()) + "=" + URLEncoder.encode(value.toString());
}
QueryString(){
query = "";
}
public synchronized void add(Object name,Object value){
if(!query.trim().equals("")) query += "&";
query += URLEncoder.encode(name.toString()) + "=" + URLEncoder.encode(value.toString());
}
public String getString(){
return query;
}
}
class FormPoster{
private URL url;
private QueryString query = new QueryString();
FormPoster(URL url) throws IllegalArgumentException{
if(!url.getProtocol().toLowerCase().startsWith("http")){
throw new IllegalArgumentException("Posting only works for http URLs");
}
this.url = url;
}
void add(String name,String value){
query.add(name,value);
}
URL getURL(){
return this.url;
}
InputStream post() throws IOException{
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(),"ASCII");
out.write(query.getString());
out.write("rn");
out.flush();
out.close();
return uc.getInputStream();
}
}
public class Main {
public static void main(String[] args) {
URL url;
try{
url = new URL("http://be-ride.acz.jp/bbs");
}catch(MalformedURLException e){
System.out.println(e);
return;
}
FormPoster poster = new FormPoster(url);
poster.add("na","DF");
poster.add("mail","abc@abc.com");
poster.add("msg","test");
try{
InputStream in = poster.post();
InputStreamReader r = new InputStreamReader(in);
int ch;
while((ch=r.read()) != -1){
System.out.print((char) ch);
}
System.out.println();
in.close();
}catch(IOException e){
System.err.println(e);
}
}
}
|
修改post方法如下:
InputStream post() throws IOException{
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
DataOutputStream out= new DataOutputStream(uc.getOutputStream());
out.write(query.getString());
out.write("rn");
out.flush();
out.close();
return uc.getInputStream();
}
InputStream post() throws IOException{
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
DataOutputStream out= new DataOutputStream(uc.getOutputStream());
out.write(query.getString());
out.write("rn");
out.flush();
out.close();
return uc.getInputStream();
}