简体   繁体   English

以URL为参数的HTTP Post请求

[英]HTTP Post request with URL as parameter

I need send https request to server using parameters one of them is URL: I do next: 我需要使用参数之一将https请求发送到服务器,其中一个是URL:接下来,我要执行以下操作:

HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(APIURL);
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("url", "https://api/v1/pictureadress/id"));
 ...

And I got error when add URL parameter. 并且添加URL参数时出现错误。 But if I add other parameters, except url, like age, gender etc. I have no errors. 但是,如果我添加其他参数(URL,年龄,性别等),则没有错误。 WHat I do wrong? 我做错了什么?

I have no exposure to apache component libraries. 我没有接触过apache组件库。 But looking at the api on BasicNameValuePair , I understand that the input name and values are stored raw and not encoded . 但是查看BasicNameValuePair上的api,我知道输入名称和值是原始存储的,而不是编码的 I fear that is the reason why your url parameter's value threw an error. 我担心这就是您的url参数的值引发错误的原因。 You may require UrlEncodedFormEntity unless already using to handle url encoded name value pairs. 您可能需要UrlEncodedFormEntity,除非已经用于处理url编码的名称/值对。

Try this.. 尝试这个..

public void postData() throws Exception {


 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM