此代码用于outlook插件。我们正在尝试发布到一个页面,并收到此错误:
The remote server returned an error: (422) Unprocessable Entity.C#代码如下:
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);知道是什么原因造成的吗?
发布于 2011-01-26 18:00:41
由于其功能有限,我避免使用WebClient,而改用WebRequest。代码如下:
CookieContainer来存储我们拾取的任何cookie,试一下下面的方法,看看它是否适用于你。
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...制片人:Hanselman,Simon (SO问题)
发布于 2011-01-30 04:38:22
这是RoR应用程序告诉您尚未形成它可以处理的请求;目标脚本存在(否则您将看到404),请求正在被处理(否则您将获得400错误),它已被正确编码(否则您将获得415错误),但实际的指令无法执行。
看着它,你似乎正在加载一些电子邮件信息。RoR应用程序可能会告诉您用户名和密码错误,或者用户不存在,或者其他什么。这取决于RoR应用程序本身。
我认为代码本身是好的;只是另一端的应用程序对你要求它做的事情并不满意。您是否在请求信息中遗漏了其他内容,如命令?(例如command=getnetemails&email=e2@email.com&password=hunter2)你确定你传递的电子邮件/密码组合是正确的吗?
有关422错误的更多信息,请参阅here。
发布于 2011-01-30 16:15:56
如果要发送的字符不在ASCII范围内,则POST数据必须在以ASCII格式在线路上发送之前进行编码。你应该尝试类似这样的东西:
Byte[] postData = asciiEncoding.GetBytes(HttpUtility.UrlEncode("email=e2@email.com&password=hunter2"));https://stackoverflow.com/questions/4600494
复制相似问题