POST请求中的参数在接收端自动更改

时间:2017-11-17 14:30:19

标签: java eclipse post httpclient

我正在使用HTTPClient和Eclipse为我们自己的软件生成POST请求。

这是我的POST请求代码:

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
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.auth.BasicScheme;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class Posts {


    public void sendPost() throws ClientProtocolException, IOException, AuthenticationException{


    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("http://localhost:8080/zarr-web/command/import/BDP?deltaImport=true");

    // Request parameters and other properties.
    List<NameValuePair> params = new ArrayList<NameValuePair>(2);
    params.add(new BasicNameValuePair("fileName", "D:\\\\Colibri_workspace\\colibri_trunk\\zarr\\zarr-backend\\servers\\wildfly-8.2.0.Final\\bin\\presentation\\soap_automation\\BANKDIRECTORYPLUS_V3_DAILY_DELTA_20170930.txt"));

     UsernamePasswordCredentials creds
     = new UsernamePasswordCredentials("administrator", "administrator");

    httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    httppost.addHeader("Content-Type", "text/plain");
    httppost.addHeader(new BasicScheme().authenticate(creds, httppost, null));

    //Execute and get the response.
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            // do something useful
        } finally {
            instream.close();
        }
    }

    }
}

但是当我在发送方发送请求时,会发送正确的“文件名”,但是当在另一方收到请求时,“文件名”变为:

  

文件名= d%3A%5C%5CColibri_workspace%5Ccolibri_trunk%5Czarr%5Czarr-后端%5Cservers%5Cwildfly-8.2.0.Final%5Cbin%5Cpresentation%5Csoap_automation%5CBANKDIRECTORYPLUS_V3_DAILY_DELTA_20170930.txt

当我使用SOAPUI软件发送请求时,文件名与发送的文件名相同。我在这里错过了什么吗?

请帮忙。

1 个答案:

答案 0 :(得分:0)

是的,您确实错过了您发送的正文是URL编码。 你明确地做了:

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

我打赌因为UrlEncodedFormEntity实际“内容类型”标题为application/x-www-form-urlencoded

您可以通过启动任何类型的TCP监视器(例如Tcpmon)来检查您实际发送到服务器的内容。然后比较代码发送的内容和SOAPUI的作用。

您的示例的URL解码值实际上是您发送的内容:

fileName=D:\\Colibri_workspace\colibri_trunk\zarr\zarr-backend\servers\wildfly-8.2.0.Final\bin\presentation\soap_automation\BANKDIRECTORYPLUS_V3_DAILY_DELTA_20170930.txt

UPD: 首先,尝试制作另一个而不是UrlEncodedFormEntity。例如SringEntity。如果您只有一个参数,则只需“按原样”传递文件名参数:

喜欢

"fileName=D:\\\\Colibri_workspace\\colibri_trunk\\zarr\\zarr-backend\\servers\\wildfly-8.2.0.Final\\bin\\presentation\\soap_automation\\BANKDIRECTORYPLUS_V3_DAILY_DELTA_20170930.txt"

如果您有更多参数,则需要用&字符分隔它们,例如:

{param1Name} = {param1Value}&amp; {param2Name} = {param2Value}等等。

但请记住,如果你的价值将&全部被破坏。

我可以说你的服务器端软件不好。它需要形式的主体(param = value)但违反HTTP协议...