在这篇文章中,我们将带领您了解HTTPPOST返回错误:417“预期失败”。的全貌,包括http返回401的相关情况。同时,我们还将为您介绍有关AndroidHTTPPost返回错误“方法不允许”、A
在这篇文章中,我们将带领您了解HTTP POST 返回错误:417“预期失败”。的全貌,包括http返回401的相关情况。同时,我们还将为您介绍有关Android HTTPPost返回错误“方法不允许”、Android HTTPPost返回错误“方法不允许。”、Angular HttpClient“解析期间的Http失败”、Angular HttpClient“解析过程中的Http失败”的知识,以帮助您更好地理解这个主题。
本文目录一览:- HTTP POST 返回错误:417“预期失败”。(http返回401)
- Android HTTPPost返回错误“方法不允许”
- Android HTTPPost返回错误“方法不允许。”
- Angular HttpClient“解析期间的Http失败”
- Angular HttpClient“解析过程中的Http失败”
HTTP POST 返回错误:417“预期失败”。(http返回401)
当我尝试 POST 到 URL 时,会导致以下异常:
远程服务器返回错误:(417) 预期失败。
这是一个示例代码:
var client = new WebClient();var postData = new NameValueCollection();postData.Add("postParamName", "postParamValue");byte[] responseBytes = client.UploadValues("http://...", postData);string response = Encoding.UTF8.GetString(responseBytes); // (417) Expectation Failed.
使用一HttpWebRequest/HttpWebResponse
对或一个HttpClient
并没有什么不同。
是什么导致了这个异常?
答案1
小编典典System.Net.HttpWebRequest 向每个请求添加标头“HTTP
标头“期望:100-继续”,除非您通过将此静态属性设置为
false 明确要求不要这样做:
System.Net.ServicePointManager.Expect100Continue = false;
一些服务器在该标头上阻塞并发送回您看到的 417 错误。
试一试。
Android HTTPPost返回错误“方法不允许”
我正在编写一个Android 2.2应用程序,该应用程序将JSON严格性过帐到ReSTfull Web服务。
Fiddler对Web服务的调用具有与预期相同的Json返回,而对ASPX Web应用程序具有与预期的相同Json返回。
当我查看服务器日志时,可以看到服务器使用307重定向响应初始POST动词,然后立即响应GET和405错误。
Fiddler和aspx应用程序记录一个307重定向的POST,然后立即另一个POST和200 OK。
到底是怎么回事?
这是主要活动:
package com.altaver.android_PostJson2;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class PostJson extends Activity {
private static final String TAG = "MainActivity";
private static final String URL = "http://web2.altaver.com/sdz/avReSTfulLogin1";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSONObject jsonObjSend = new JSONObject();
try {
jsonObjSend.put("Pass","sz");
jsonObjSend.put("User","szechman");
Log.i(TAG,jsonObjSend.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonObjRecv = HttpClient.SendHttpPost(URL,jsonObjSend);
//examine JSONObject later
}
}
这是进行Web服务调用的类代码:
package com.altaver.android_PostJson2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.util.Log;
public class HttpClient {
private static final String TAG = "HttpClient";
public static JSONObject SendHttpPost(String URL,JSONObject jsonObjSend) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpClientParams.setRedirecting(httpclient.getParams(),true);
//added cookie policy,wild shot in the dark
//httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,>CookiePolicy.RFC_2109);
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
//httpPostRequest.setHeader("User-Agent",>"com.altaver.android_PostJson2");
httpPostRequest.setHeader("User-Agent","Mozilla/5.0 (Windows; U; >Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401");
httpPostRequest.setHeader("Accept","application/json");
httpPostRequest.setHeader("Content-Type","application/json");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) >httpclient.execute(httpPostRequest);
Log.i(TAG,"HTTPResponse received in [" + >(System.currentTimeMillis()-t) + "ms]");
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
String resultString= convertStreamToString(instream);
instream.close();
resultString = resultString.substring(1,resultString.length()-1); // >remove wrapping "[" and "]"
JSONObject jsonObjRecv = new JSONObject(resultString);
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the >BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a >StringBuilder
* and returned as String.
*
* (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01>/11/a-simple-restful-client-at-android/
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Android HTTPPost返回错误“方法不允许。”
我正在编写一个Android 2.2应用程序,该应用程序将JSON严格性过帐到ReSTfull Web服务。
Fiddler对Web服务的调用具有与预期相同的Json返回,而对ASPX Web应用程序具有与预期的相同Json返回。
当我查看服务器日志时,可以看到服务器使用307重定向响应初始POST动词,然后立即响应GET和405错误。
Fiddler和aspx应用程序记录一个307重定向的POST,然后立即另一个POST和200 OK。
到底是怎么回事?
这是主要活动:
package com.altaver.android_PostJson2;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class PostJson extends Activity {
private static final String TAG = "MainActivity";
private static final String URL = "http://web2.altaver.com/sdz/avReSTfulLogin1";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSONObject jsonObjSend = new JSONObject();
try {
jsonObjSend.put("Pass","sz");
jsonObjSend.put("User","szechman");
Log.i(TAG,jsonObjSend.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonObjRecv = HttpClient.SendHttpPost(URL,jsonObjSend);
//examine JSONObject later
}
}
这是进行Web服务调用的类代码:
package com.altaver.android_PostJson2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.util.Log;
public class HttpClient {
private static final String TAG = "HttpClient";
public static JSONObject SendHttpPost(String URL,JSONObject jsonObjSend) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpClientParams.setRedirecting(httpclient.getParams(),true);
//added cookie policy,wild shot in the dark
//httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,>CookiePolicy.RFC_2109);
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
//httpPostRequest.setHeader("User-Agent",>"com.altaver.android_PostJson2");
httpPostRequest.setHeader("User-Agent","Mozilla/5.0 (Windows; U; >Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401");
httpPostRequest.setHeader("Accept","application/json");
httpPostRequest.setHeader("Content-Type","application/json");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) >httpclient.execute(httpPostRequest);
Log.i(TAG,"HTTPResponse received in [" + >(System.currentTimeMillis()-t) + "ms]");
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
String resultString= convertStreamToString(instream);
instream.close();
resultString = resultString.substring(1,resultString.length()-1); // >remove wrapping "[" and "]"
JSONObject jsonObjRecv = new JSONObject(resultString);
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the >BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a >StringBuilder
* and returned as String.
*
* (c) public domain: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01>/11/a-simple-restful-client-at-android/
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Angular HttpClient“解析期间的Http失败”
我尝试从 Angular 4 向我的 Laravel 后端发送一个 POST 请求。
我的 LoginService 有这个方法:
login(email: string,password: string) {
return this.http.post(`http://10.0.1.19/login`,{ email,password })
}
我在我的 LoginComponent 中订阅了这个方法:
.subscribe(
(response: any) => {
console.log(response)
location.reload()
},(error: any) => {
console.log(error)
})
这是我的 Laravel 后端方法:
...
if($this->auth->attempt(['email' => $email,'password' => $password],true)) {
return response('Success',200);
}
return response('Unauthorized',401);
我的 Chrome 开发工具显示我的请求成功,状态码为 200。但是我的 Angular 代码触发了这个error
块并给了我这个消息:
解析http://10.0.1.19/api/login时的 Http
失败
如果我从后端返回一个空数组,它可以工作......所以Angular试图将我的响应解析为JSON?我怎样才能禁用它?
Angular HttpClient“解析过程中的Http失败”
我的LoginService有这个方法:
login(email: string,password: string) { return this.http.post(`http://10.0.1.19/login`,{email,password}) }
我在LoginComponent上订阅了这个方法:
.subscribe( (response: any) => { console.log(response) location.reload() },(error: any) => { console.log(error) } )
这是我的Laravel后端方法:
... if($this->auth->attempt(['email' => $email,'password' => $password],true)) { return response('Success',200); } return response('Unauthorized',401);
我的chrome dev工具说我的请求是200个状态代码的成功.但我的Angular代码触发了错误块并给出了我的这条消息:
“解析http://10.0.1.19/api/login期间的Http失败”
如果我从我的后端返回一个空数组,它可以工作……所以Angular试图解析我对json的响应?我怎么能这个呢?
在您的示例中,您应该能够使用:
return this.http.post('http://10.0.1.19/login',password},{responseType: 'text'})
关于HTTP POST 返回错误:417“预期失败”。和http返回401的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Android HTTPPost返回错误“方法不允许”、Android HTTPPost返回错误“方法不允许。”、Angular HttpClient“解析期间的Http失败”、Angular HttpClient“解析过程中的Http失败”等相关内容,可以在本站寻找。
本文标签: