无法在java中发出http POST请求?
我仍然无法发出请求。您正在成功收到一个响应,该响应不包含“logontoken”标题。很可能是因为响应不是HTTP 200 OK响应。为什么?我们不知道,这完全取决于服务器在HTTP之上实现的协议无法在java中发出http POST请求?,java,http-post,Java,Http Post,我仍然无法发出请求。您正在成功收到一个响应,该响应不包含“logontoken”标题。很可能是因为响应不是HTTP 200 OK响应。为什么?我们不知道,这完全取决于服务器在HTTP之上实现的协议 话虽如此,使用httppost.setEntity(新的UrlEncodedFromEntity(后参数))和httppost.addHeder(“内容类型”,“应用程序/json”)对我来说并不合适。URL编码的表单实体不是json内容类型。因此,要么将您的post参数转换为json,要么丢失内容类
话虽如此,使用
httppost.setEntity(新的UrlEncodedFromEntity(后参数))
和httppost.addHeder(“内容类型”,“应用程序/json”)
对我来说并不合适。URL编码的表单实体不是json内容类型。因此,要么将您的post参数转换为json,要么丢失内容类型标题。请检查我发布的替代方法,无法添加请求正文,如何实现此目的?@“我无法发出请求”不是问题的描述。你需要准确地解释问题是什么。至少,您需要检查响应状态,并告诉我们它是什么。HTTP/1.1 401 Unauthorized是我获得@Mike Nakis的状态
static HttpClient httpclient = new DefaultHttpClient();
static HttpPost httppost = new HttpPost("http://servername:6405/biprws/logon/long");
public static void main(String[] args) throws ClientProtocolException, IOException {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("userName", "Administrator"));
postParameters.add(new BasicNameValuePair("password", "test"));
postParameters.add(new BasicNameValuePair("auth", "secEnterprise"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
httppost.addHeader("accept", "application/json");
httppost.addHeader("Content-Type", "application/json");
HttpResponse response = httpclient.execute(httppost);
Header s = response.getFirstHeader("logontoken");
String s1 = s.getValue();
System.out.println(s1);// null pointer exception here
}
HttpClient client1 = new DefaultHttpClient();
HttpPost post = new HttpPost("http://servername:6405/biprws/logon/long");
String json = "{\"UserName\":\"Administrator\",\"Password\":\"test\",\"Auth\":\"secEnterprise\"}";
StringEntity entity = new StringEntity(json,"UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);
System.out.println(entity);
post.setHeader("Accept", "application/json");
HttpResponse response = client1.execute(post);
BufferedReader rd1 = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String result1 = null;
String line1 = "";
result1 = rd1.readLine();
System.out.println(result1);