首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取此错误:“远程服务器返回错误:(422)无法处理的实体。”执行从C#到RoR的post时

获取此错误:“远程服务器返回错误:(422)无法处理的实体。”执行从C#到RoR的post时
EN

Stack Overflow用户
提问于 2011-01-05 11:08:42
回答 5查看 10.6K关注 0票数 2

此代码用于outlook插件。我们正在尝试发布到一个页面,并收到此错误:

代码语言:javascript
复制
The remote server returned an error: (422) Unprocessable Entity.

C#代码如下:

代码语言:javascript
复制
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
        Byte[] postData = asciiEncoding.GetBytes("email=e2@email.com&password=hunter2");
        char[] resultHTML = asciiEncoding.GetChars(webClient.UploadData("http://url", "POST", postData));
        string convertedResultHTML = new string(resultHTML);

知道是什么原因造成的吗?

EN

回答 5

Stack Overflow用户

发布于 2011-01-26 18:00:41

由于其功能有限,我避免使用WebClient,而改用WebRequest。代码如下:

  1. 不期望返回HTTP100状态代码,
  2. 创建一个CookieContainer来存储我们拾取的任何cookie,
  3. 设置内容长度标题,并在post数据中设置cookie中的每个值。

试一下下面的方法,看看它是否适用于你。

代码语言:javascript
复制
System.Net.ServicePointManager.Expect100Continue = false;
System.Net.CookieContainer cookies = new System.Net.CookieContainer();

// this first request just ensures we have a session cookie, if one exists
System.Net.WebRequest req = System.Net.WebRequest.Create("http://localhost/test.aspx");
((System.Net.HttpWebRequest)req).CookieContainer = cookies;
req.GetResponse().Close();

// this request submits the data to the server
req = System.Net.WebRequest.Create("http://localhost/test.aspx");
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
((System.Net.HttpWebRequest)req).CookieContainer = cookies;

string parms = string.Format("email={0}&password={1}", 
    System.Web.HttpUtility.UrlEncode("e2@email.com"),
    System.Web.HttpUtility.UrlEncode("hunter2"));
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parms);
req.ContentLength = bytes.Length;

// perform the POST
using (System.IO.Stream os = req.GetRequestStream()) 
{
   os.Write(bytes, 0, bytes.Length);
}

// read the response
string response;
using (System.Net.WebResponse resp = req.GetResponse())
{
  if (resp == null) return;
  using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
  {
      response = sr.ReadToEnd().Trim();
  }
}
// the variable response holds the results of the request...

制片人:HanselmanSimon (SO问题)

票数 1
EN

Stack Overflow用户

发布于 2011-01-30 04:38:22

这是RoR应用程序告诉您尚未形成它可以处理的请求;目标脚本存在(否则您将看到404),请求正在被处理(否则您将获得400错误),它已被正确编码(否则您将获得415错误),但实际的指令无法执行。

看着它,你似乎正在加载一些电子邮件信息。RoR应用程序可能会告诉您用户名和密码错误,或者用户不存在,或者其他什么。这取决于RoR应用程序本身。

我认为代码本身是好的;只是另一端的应用程序对你要求它做的事情并不满意。您是否在请求信息中遗漏了其他内容,如命令?(例如command=getnetemails&email=e2@email.com&password=hunter2)你确定你传递的电子邮件/密码组合是正确的吗?

有关422错误的更多信息,请参阅here

票数 1
EN

Stack Overflow用户

发布于 2011-01-30 16:15:56

如果要发送的字符不在ASCII范围内,则POST数据必须在以ASCII格式在线路上发送之前进行编码。你应该尝试类似这样的东西:

代码语言:javascript
复制
Byte[] postData = asciiEncoding.GetBytes(HttpUtility.UrlEncode("email=e2@email.com&password=hunter2"));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4600494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档