Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

java - Java-通过POST方法轻松发送HTTP参数(Java - sending HTTP parameters via POST method easily)

I am successfully using this code to send HTTP requests with some parameters via GET method

(我已成功使用此代码通过GET方法发送带有某些参数的HTTP请求)

void sendRequest(String request)
{
    // i.e.: request = "http://example.com/index.php?param1=a&param2=b&param3=c";
    URL url = new URL(request); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
    connection.setDoOutput(true); 
    connection.setInstanceFollowRedirects(false); 
    connection.setRequestMethod("GET"); 
    connection.setRequestProperty("Content-Type", "text/plain"); 
    connection.setRequestProperty("charset", "utf-8");
    connection.connect();
}

Now I may need to send the parameters (ie param1, param2, param3) via POST method because they are very long.

(现在,我可能需要通过POST方法发送参数(即param1,param2,param3),因为它们很长。)

I was thinking to add an extra parameter to that method (ie String httpMethod).

(我在想为该方法添加一个额外的参数(即String httpMethod)。)

How can I change the code above as little as possible to be able to send paramters either via GET or POST ?

(如何才能尽可能地更改上面的代码,以便能够通过GETPOST发送参数?)

I was hoping that changing

(我希望改变)

connection.setRequestMethod("GET");

to

(至)

connection.setRequestMethod("POST");

would have done the trick, but the parameters are still sent via GET method.

(本来可以解决问题的,但是参数仍然通过GET方法发送。)

Has HttpURLConnection got any method that would help?

(HttpURLConnection是否有任何HttpURLConnection方法?)

Is there any helpful Java construct?

(有没有有用的Java构造?)

Any help would be very much appreciated.

(任何帮助将不胜感激。)

  ask by dan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In a GET request, the parameters are sent as part of the URL.

(在GET请求中,参数作为URL的一部分发送。)

In a POST request, the parameters are sent as a body of the request, after the headers.

(在POST请求中,将参数作为请求的正文发送到标头之后。)

To do a POST with HttpURLConnection, you need to write the parameters to the connection after you have opened the connection.

(要使用HttpURLConnection进行POST,您需要在打开连接后将参数写入连接。)

This code should get you started:

(这段代码可以帮助您入门:)

String urlParameters  = "param1=a&param2=b&param3=c";
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
String request        = "http://example.com/index.php";
URL    url            = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
   wr.write( postData );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...