我遇到的情况是,我正在努力解决现实世界中的 REst GET 和 POST 架构哲学。
我有一个本质上是幂等的 REst 调用。它需要在其有效负载中包含复杂的数据类型(XML 中的保险单),对其执行复杂的业务逻辑并返回保费。它对状态没有任何作用,因此本质上是幂等的。
REst 调用目前是 POST。这样做的有效理由是消息正文很大,很可能会被丢弃并在 Internet Explorer 中变得混乱。然而,它也是幂等的,并且从根本上违反了 GET 与 POST 的原则。
以前有人遇到过这个难题吗?谢谢。
HTTP 协议提供了哪些可能性来将数据和参数从客户端传输到服务器?
Content-TypeHTTP 正文,其内容类型通过标头参数(FormParams 和任何其他发布数据)定义这是正确和完整的吗?
在对如何执行此操作感到困惑之后(如此处和此处所示),我现在使用以下代码成功连接到我的服务器应用程序和适当的 RESTful 方法:
public void onFetchBtnClicked(View v){
if(v.getId() == R.id.FetchBtn){
Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
new CallAPI().execute("http://10.0.2.2:28642/api/Departments/GetCount?serialNum=4242");
}
}
public static class CallAPI extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String urlString=params[0]; // URL to call
String resultToDisplay = "";
InputStream in = null;
// HTTP Get
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception e ) {
System.out.println(e.getMessage()); …Run Code Online (Sandbox Code Playgroud) 我尝试通过 Postman 向设备的 Restful API 发送 HTTP Get,它工作正常,返回了我期望的所有文本。Postman 建议该请求的 Ruby 代码如下:
url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request.basic_auth 'admin', 'admin'
request["accept"] = 'Application/json'
response = http.request(request)
puts response.read_body
Run Code Online (Sandbox Code Playgroud)
但是当我在代码中尝试这样做时,它返回了截断的响应(缺少行),并且我必须多次重新发送相同的 Get 才能获取整个文本响应响应。
上面的 Ruby 代码中是否缺少任何内容导致响应被截断?
更新1
我试过这个
url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request.basic_auth 'admin', 'admin'
request["accept"] = 'Application/json'
response = http.request(request)
puts response.read_body
response.read_body do |segment|
puts segment.to_s
end
Run Code Online (Sandbox Code Playgroud)
并产生了这个错误
IOError (Net::HTTPOK#read_body called twice):
Run Code Online (Sandbox Code Playgroud)
更新2
我试过这个
1073 url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
1074 http …Run Code Online (Sandbox Code Playgroud) 在我的 AngularJS 应用程序中,我发送 HTTP GET 请求,如下所示。
MyService.HttpReq("testUrl", "GET", null);
Run Code Online (Sandbox Code Playgroud)
HttpReq方法在服务中定义并实现如下:
this.HttpReq = function(URL, method, payload)
{
$http({
url: URL,
method: method,
cache: false,
data: postData,
headers: {
'Content-Type': 'application/json',
}
}).success(function(response)
{
console.log("Success: "+JSON.stringify(response));
}).error(function(data, status)
{
console.error("Error");
});
}
Run Code Online (Sandbox Code Playgroud)
首先,这是在 AngularJS 中发送 HTTP 请求的正确方法吗?
我面临的问题是,有时我得到缓存数据作为响应,但 HTTP 请求没有到达服务器。可能是什么问题?
更新
根据评论和回答,我已更新了我的 HTTP 请求代码,如下所示,但仍然遇到相同的问题。
this.HttpReq = function(URL, method, payload)
{
$http({
url: URL,
method: method,
cache: false,
data: payload,
headers: {
'Content-Type': 'application/json',
'Cache-Control' : 'no-cache'
}
}).
then(
function(response)
{ …Run Code Online (Sandbox Code Playgroud) 所以我试图创建一个URL来使用httpget,下载页面源.但我每次运行应用程序时都遇到问题,它说我在字符串/ uri中有一个非法字符.这是我尝试过的代码.
String Search = "http://www.lala.com/";
Run Code Online (Sandbox Code Playgroud)
也,
HttpGet request = new HttpGet("http://www.lala.com/");
Run Code Online (Sandbox Code Playgroud)
每次我尝试,
Uri search = new Uri("http://www.lala.com/");
Run Code Online (Sandbox Code Playgroud)
我得到"无法实例化Uri".
我不确定我做错了什么,这也是获取页面源代码.
try
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.lala.com/");
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
}
catch (IOException e1)
{
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!〜坦纳.
(lala.com不是我使用的网站顺便提一下:P)
我在使用winsock读取一些分块的HTTP响应数据时遇到了麻烦.我发送请求很好并获得以下回复:
HTTP/1.1 200 OK
Server: LMAX/1.0
Content-Type: text/xml; charset=utf-8
Transfer-Encoding: chunked
Date: Mon, 29 Aug 2011 16:22:19 GMT
Run Code Online (Sandbox Code Playgroud)
使用winsock recv.在这一点上,它只是挂起.我让听众在一个无限循环中运行,但没有任何东西被拾起.
我认为这是一个C++问题,但它也可能与我通过stunnel推送连接以将其包装在HTTPS中的事实有关.我有一个测试应用程序使用C#中的一些库,它通过stunnel完美地工作.我很困惑为什么我的循环在初始recv之后没有收到C++分块数据.
这是有问题的循环...它是在上面的chunked ok响应之后调用的...
while(true)
{
recvBuf= (char*)calloc(DEFAULT_BUFLEN, sizeof(char));
iRes = recv(ConnectSocket, recvBuf, DEFAULT_BUFLEN, 0);
cout << WSAGetLastError() << endl;
cout << "Recv: " << recvBuf << endl;
if (iRes==SOCKET_ERROR)
{
cout << recvBuf << endl;
err = WSAGetLastError();
wprintf(L"WSARecv failed with error: %d\n", err);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试将我从GET请求收到的响应json_decode到我的服务器端API,但我得到一个空字符串.我是否正确地假设因为响应包含JSON解码器无法处理的所有标头信息?这是我从服务器获得的完整响应:
HTTP/1.1 200 OK
Server: nginx/1.0.5
Date: Sun, 18 Mar 2012 19:44:43 GMT
Content-Type: application/json
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: Servlet/3.0; JBossAS-6
Content-Length: 97
{"pid":"162000798ab8481eaeb2b867e10f8849","uuid":"973b8722c75a4cacb9fd2316517587bb"}
Run Code Online (Sandbox Code Playgroud)
在将响应发送到客户端之前,是否需要删除servlet中的标头?
我正在尝试从Windows Phone应用程序的公共API收集数据.
private void GatherPosts()
{
string url = baseURL + "?after=" + lastPostId + "&gifs=1";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "text/json";
request.Method = "GET";
AsyncCallback callback = new AsyncCallback(PostRequestFinished);
request.BeginGetResponse(callback, request);
}
private void PostRequestFinished(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
}
Run Code Online (Sandbox Code Playgroud)
但是我一直在ProtocolViolationException使用该消息获得回调方法的最后一行A request with this method cannot have a request body..我读到这是因为我正在尝试发送数据,这显然是禁止GET协议的,但我没有看到我在做什么,即如何避免它.
当我在Hero.service.ts中使用get url作为web api时,在浏览器控制台中出现以下错误.但是当我在一个项目中使用.Json文件时它工作正常,因为我能够在输出页面中看到值.我刚刚在angular.io网站上学习.
以下是控制台中的错误:
angular2-polyfills.js:471 Error: Uncaught (in promise): EXCEPTION: Error: unable to parse url 'http://md5.jsontest.com/?text=example_text'; original error: Cannot read property 'split' of undefined in [null](…)consoleError @ angular2-polyfills.js:471
Get url: http://md5.jsontest.com/?text=example_text
Run Code Online (Sandbox Code Playgroud)
这是完整的代码,
你能帮我解决一下我在做错的事吗?