简体   繁体   English

将curl转换为Java HttpPost

[英]Convert curl to Java HttpPost

I'm having some trouble converting a curl command to a java httpPost. 我在将curl命令转换为Java httpPost时遇到一些麻烦。 I'm trying to contact the Census Bureau Address Batch endpoint. 我正在尝试联系人口普查局地址批处理端点。 I can contact it but I get a response stating something is incorrect. 我可以联系它,但是我得到答复说有些不正确。

The curl command is on the last page of this https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf curl命令位于此https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf的最后一页

The required parameters state it needs benchmark, vintage and addressFile so I'm trying to use each. 必需的参数说明它需要基准,vintage和addressFile,因此我尝试使用它们。 The addressFile itself is a csv that I got here https://geocoding.geo.census.gov/geocoder/locations/addressbatch?form addressFile本身是一个csv,我在这里https://geocoding.geo.census.gov/geocoder/locations/addressbatch?form

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response;

try {
    String url = "https://geocoding.geo.census.gov/geocoder/geographies/addressbatch"
    HttpPost httpPost = new HttpPost(url);

    MultipartEntity postEntity = new MultipartEntity();
    postEntity.addPart("addressFile", new FileBody(new File("addresses.csv")));
    postEntity.addPart("benchmark", new StringBody("Public_AR_Current"));
    postEntity.addPart("vintage", new StringBody("Public_AR_Current"));

    httpPost.setEntity(postEntity);

    response = httpClient.execute(httpPost);
    HttpEntity responseEntity = response.getEntity();

    StringWriter writerResponse = new StringWriter();
    IOUtils.copy(responseEntity.getContent(), writerResponse, "UTF-8");

    print response.getStatusLine()

    print writerResponse.toString()

} catch (IOException e) {
    print e
}
finally {
    httpClient.getConnectionManager().shutdown();
}

So doing it like this I get a code 400 error 所以这样做我得到一个代码400错误

HTTP/1.1 400 Bad Request HTTP / 1.1 400错误请求

While attempting to geocode your batch input, an error occurred validating and processing the parameters that were provided. 尝试对批处理输入进行地理编码时,验证和处理提供的参数时发生错误。

Please validate the benchmark, vintage (if this is a geographies batch geocode request), and addressFile parameter values that are being used and retry your batch geocode request. 请验证基准,年份(如果这是地理批次地理编码请求)和正在使用的addressFile参数值,然后重试您的批次地理编码请求。

More information and documentation (available in HTML and PDF formats) about the Census Geocoder and how to use it can be found here: https://geocoding.geo.census.gov/geocoder/'> https://geocoding.geo.census.gov/ 有关人口普查地理编码器及其使用方法的更多信息和文档(以HTML和PDF格式提供)可以在以下位置找到: https://geocoding.geo.census.gov/geocoder/ '> https://geocoding.geo。 census.gov/

Any thoughts on what I'm doing incorrectly? 对我做错的事情有什么想法吗? The response isn't very specific as to what I'm doing wrong. 对于我做错了什么,反应不是很具体。

Turns out the API instructions are incorrect. 原来API指示不正确。 The vintage and benchmark parameters need strings with integer values rather than strings with the names of the benchmark/vintages. 年份和基准参数需要具有整数值的字符串,而不是具有基准/年份名称的字符串。

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

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