通过 HTTP 客户端编写 curl 等效 java 代码时遇到问题
Facing issue while writing curl equivalent java code through HTTP Client
我有一个 CURL 命令,当我执行它时它工作正常。下面是 curl 命令。如果我尝试使用 HTTP 客户端在 java 中编写相同的 curl 命令,我将收到 HTTP 403。我无法理解为什么在执行 CURL 时我没有收到相同的错误,但在代码中我收到了.有人可以帮我修复我的代码吗?
curl -v -X POST "http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28" -d '{"providerType":"MOTOID","email":"1234@idmtest.com","password":""}'
下面是我的Java代码。
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28");
post.addHeader("User-Agent","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
post.addHeader("Host","iapi-va3.svcmot.com");
post.addHeader("Accept","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
post.addHeader("Accept", "*/*");
post.addHeader("Content-Length", "66");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
Header[] headerArray=post.getAllHeaders();
for(Header h:headerArray)
{
System.out.println(h.getName()+"---"+h.getValue());
}
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("providerType", "MOTOID"));
nameValuePairs.add(new BasicNameValuePair("email", "1234@idmtest.com"));
nameValuePairs.add(new BasicNameValuePair("password", ""));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
System.out.println("\n"+response);
//System.out.println(response.getStatusLine());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("\n"+line);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
有一个区别,您从 curl 向服务器发送 json,但在您的程序中,您将其作为参数发送。
显然您需要在发送前在您的应用中将参数序列化为 json。
我有一个 CURL 命令,当我执行它时它工作正常。下面是 curl 命令。如果我尝试使用 HTTP 客户端在 java 中编写相同的 curl 命令,我将收到 HTTP 403。我无法理解为什么在执行 CURL 时我没有收到相同的错误,但在代码中我收到了.有人可以帮我修复我的代码吗?
curl -v -X POST "http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28" -d '{"providerType":"MOTOID","email":"1234@idmtest.com","password":""}'
下面是我的Java代码。
public static void main(String[] args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://iapi-va3.svcmot.com/v1/gui/user.json?appid=4KCPMNRRT4VJBY54V3888YUOLHLICK28");
post.addHeader("User-Agent","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
post.addHeader("Host","iapi-va3.svcmot.com");
post.addHeader("Accept","curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");
post.addHeader("Accept", "*/*");
post.addHeader("Content-Length", "66");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
Header[] headerArray=post.getAllHeaders();
for(Header h:headerArray)
{
System.out.println(h.getName()+"---"+h.getValue());
}
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("providerType", "MOTOID"));
nameValuePairs.add(new BasicNameValuePair("email", "1234@idmtest.com"));
nameValuePairs.add(new BasicNameValuePair("password", ""));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
System.out.println("\n"+response);
//System.out.println(response.getStatusLine());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("\n"+line);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
有一个区别,您从 curl 向服务器发送 json,但在您的程序中,您将其作为参数发送。
显然您需要在发送前在您的应用中将参数序列化为 json。