这是方法。我已经研究了很长一段时间,并修改了标题值等。除非我包含内容长度标题,否则它仍然会出现401 Unauthorized Exception,然后它会出现另一个错误,说内容长度标题无效。有谁知道如何解决这个问题?
我也试过通过HTTPWebRequest做到这一点。
Webclient代码:
public void postResponse(string supplierid, string token, string geturl, string lineid)
{
lineid = lineid.Trim();
//string postdata = ("{'supplier_id':'"+supplierid+"', 'token':'"+token+"','ci_lineitem_ids':["+lineid+"]}");
try
{
string postdata = ("{'supplier_id':'"+supplierid+"','token':'"+token+"','ci_lineitem_ids':["+lineid+"]}");
Console.WriteLine(postdata);
WebClient postWithParamsClient = new WebClient();
postWithParamsClient.UploadStringCompleted +=
new UploadStringCompletedEventHandler(postWithParamsClient_UploadStringCompleted);
postWithParamsClient.UseDefaultCredentials = true;
postWithParamsClient.Credentials = new NetworkCredential(supplierid, token);
postWithParamsClient.Headers.Add("Content-Type", "application/json");
string headerlength = postdata.Length.ToString();
//postWithParamsClient.Headers["Content-Length"] = headerlength;
postWithParamsClient.UploadStringAsync(new Uri(geturl),
"POST",
postdata);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
HttpWebRequest的
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] postBytes = encoding.GetBytes(postdata);
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(geturl);
request.Credentials = new NetworkCredential(supplierid, token);
request.Method = "POST";
request.ContentLength = postBytes.Length;
request.ContentType = "application/json";
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Close();
答案 0 :(得分:0)
您应该尝试使用fiddler来查看您发送的内容并与服务器所需的内容进行比较。检查授权标头和凭据的有效性。我认为这是第一件要做的事情