- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在按照 fliptop 的说明尝试使用 API .api的描述中指定它必须使用json输入,因此,我制作了json对象并自己将其转换为字符串。但是,当我尝试使用 find_contact api 时,http 响应为
{"param":500}
而不是像
这样的正确数据<强>{ “帐户”: { "site_key": "...", "seat_id": "...", "org_id": "", “执照”: {....
我尝试通过发送带有不正确输入的请求在文档网站上做一些测试(点击任何高级 api,你会看到它),它给了我相同的响应正文{ “参数”:500}。因此,我认为我的请求实体可能有问题。
这是我的代码。谁能告诉我这是怎么回事?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class Test {
// Generate json string
// {"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"San Francisco"} ] }
public static String jsonString() {
JSONObject obj1 = new JSONObject();
obj1.put("email", "doug@fliptop.com");
obj1.put("title", "CEO");
obj1.put("company", "Fliptop");
obj1.put("address", "San Francisco");
JSONArray list1 = new JSONArray();
list1.add(obj1);
JSONObject obj2 = new JSONObject();
obj2.put("contacts", list1);
System.out.println(obj2.toJSONString());
return obj2.toJSONString();
}
public static void find_contact() {
HttpClient httpClient = new DefaultHttpClient();
try {
//you will need api key here!!
HttpPost request = new HttpPost("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
StringEntity params = new StringEntity(jsonString(),"UTF-8");
params.setContentType("application/json; charset=UTF-8");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String result = "";
String line;
while ((line = rd.readLine()) != null) {
result += line;
}
System.out.println(result);
rd.close();
System.out.println("status:" + response.getStatusLine());
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
find_contact();
}
}
更新:
我听从了 Dave 的建议,通过 WireShark 检查请求。请求显示为POST请求,数据为text/plain,内容如下
json={"contacts": [ { "email": "doug@fliptop.com", "title":"CEO", "company":"Fliptop", "address":"旧金山"} ] } .
我不是网络专家,但这个请求对我来说似乎没问题。不过,我对发生的事情感到困惑...
最佳答案
经过几个小时的努力,我通过检查来自 chrome 的发布请求的详细信息发现了我的问题的原因。您可以看到 Content-Type 实际上是 "application/x-www-form-urlencoded",这就是我一直失败的原因。通过更改我的 Content-Type,一切正常。
这是我的代码,您可以使用 URL 或 appache httpclient,它们都可以正常工作。
URL url = new URL("http://api.fliptop.com/s2/v1/find_contact?api_key=xxxxxxxxxxxxxxxxxxxxxx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data); // data = "json={\"contact\": { \"email\": \"robbie@fliptop.com\"}}"
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader reader =
new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
}
关于java - HttpPost 请求未发送正确的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18081669/
我不明白为什么需要使用 [HttpPost("[action]")] 而不是 [HttpPost]? 如果使用[HttpPost("[action]")],那么来自角度客户端的请求会触发 Contro
我不明白为什么需要使用 [HttpPost("[action]")] 而不是 [HttpPost]? 如果使用[HttpPost("[action]")],那么来自角度客户端的请求会触发 Contro
所以我们有[HttpPost],这是一个可选属性。我知道这会限制调用,因此只能通过 HTTP POST 请求进行调用。我的问题是我为什么要这样做? 最佳答案 想象一下以下情况: [HttpGet] p
在 Scott Hanselman 的书中(第 1 章),他为我们提供了两个选项来实现 Create 操作方法的 [HttpPost]。 第一个依赖于 TryUpdateModel 根据传入的表单字段
我正在从 Android 应用程序向服务器发送 JSON 数据,我在人们添加的示例中看到: httpPost.setHeader("Content-type", "application/json")
- - - 更新 能够通过使用 UsernamePasswordCredentials 类来修复它 代码如下所示 val 客户端 = 新的 DefaultHttpClient client.getCr
这个问题让我很恼火。我正在重复使用适用于其他项目/类但不适用于我当前项目的相同代码。我所做的只是更改变量(POST 值和 EditText 名称)。但没有成功。 import java.util.Ar
这是我的代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
我正在为黑莓开发应用程序,我需要向我的服务器发送一个 Http 发布请求。我正在使用模拟器来测试我的应用程序,我发现这段代码是为了发送请求: http://vasudevkamath.techfiz.
我希望能够使用参数查看完整的 HttpClient/HttpPost URI。我不确定如何将其输出到我的控制台。 HttpClient httpclient = HttpClients.createD
我有一个带有 httpPost 属性的 ActionResult。如果我没有记错的话,当用户从 DateTimePicker 中选择日期时,我需要此属性。就我而言,我有 2 个日期时间选择器,类型为
嗨:)我遇到了一个简单的问题,但非常烦人。我正在尝试使用 HttpPost 类发送 http post 请求 这是返回InputStream的方法的一部分: HttpClient
我正在尝试通过 android studio 中的 java 将发布请求发送到我的 PHP 页面 首先我尝试了 httppost,但按下该键后应用程序崩溃了 然后我读到我必须使用新线程,我这样做了,但
我正在尝试将我的应用程序设置为在 HttpPost 上 3 秒后抛出超时异常。我正在从 ASyncTask 执行此请求。出于某种原因,即使我给它一个不存在的域,它也会挂起大约一两分钟,然后抛出最后一个
我正在尝试从 Android 应用程序中向我的 Google App Engine 应用程序发送 POST 请求,以便将文件上传到 blobstore 并将一些元数据上传到数据存储区。我构建了一个可以
在我的 CountryActivity.java 中,我有一个 HttpRequest 来检索维基百科的 json 信息。这是我使用 AsyncTask 的代码: private class D
我的 MainAcitivity 中有以下代码,用于使用方法 jsnrqstr.sendRequest(username, password, URL); 获取的用户名和密码登录到应用程序/p> lo
我有一个网页用 1 (true) 或 0 (false) 响应日志记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,它可以是 0 或 1。但是当我用 Android 应用程序调用它时,结果总是
在我的应用程序中,我必须从 4 个不同的 url 获取数据,然后在获取数据完成后,我必须按特定顺序显示项目。我正在使用 HttoPost 发送发布请求。我在不同的线程中分别发送 4 个请求。当线程获取
我尝试了所有可能的方法,但仍然出现以下错误 -1.无法解析符号ClientProtocolException2.无法解析符号HttpPost3.无法解析符号DefaultHttpClient JSON
我是一名优秀的程序员,十分优秀!