Problems with C# HTTP Post with Authentication
I'm trying to send some XML through HTTP Post. But I'm getting 401 Unauthorized
. Funny thing is when I access the URL through the browser, the username/password I provide are OK.
Here is my method signature:
internal static string Send(string URL, string XMLdata, string RequestMethod,
string RequestUsername, string RequestPassword)
I have tried this as method body:
string requestData = XMLdata;
string responseData = "";
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(URL);
objRequest.Method = RequestMethod;
objRequest.PreAuthenticate = true;
objRequest.ContentType = "application/x-www-form-urlencoded";//"text/xml";
objRequest.Credentials = new NetworkCredential(RequestUsername, RequestPassword);;
objRequest.ContentLength = requestData.Length;
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(requestData);
}
finally
{
myWriter.Close();
}
StreamReader sr = null;
try
{
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
sr = new StreamReader(objResponse.GetResponseStream());
responseData = sr.ReadToEnd();
}
finally
{
if (sr != null)
sr.Close();
}
return responseData;
And also this:
string result;
using (var client = new WebClient())
{
Uri requestUri = new Uri(URL);
CredentialCache cache = new CredentialCache();
NetworkCredential nc = new NetworkCredential(RequestUsername, RequestPassword);
cache.Add(requestUri, "Basic", nc);
client.Credentials = cache;
result = client.UploadString(requestUri, XMLdata);
}
return result;
Both get the 401
. What am I doing wrong? Just a reminder: when I access the URL through the browser, it prompts for username/password and the credentials I provide are OK.
My readings on the subject so far:
- HttpClient and forms authentication in C#
- Login to th开发者_StackOverflow中文版e page with HttpWebRequest
- HTTP POST Returns Error: 417 "Expectation Failed."
- HTTP Post XML document - server receives only first line
- POST to HTTPS authentication error
- C# Xml in Http Post Request Message Body
I appreciate the help.
[At request of OP, I'm migrating my comment to an answer]
I don't have an answer for you, from your description, but I do have a suggestion: use Fiddler to see what the request and response look like, both when the browser request succeeds, and when the programmatic request fails. I suspect there will be a difference that jumps out at you.
I solved it. Actually I was misled by the provider to make a mistake with the URL. The reason I'm posting my answer is for future reference, if someone ever comes across this.
The provider emailed me with the URL http://api.providersurl.com
. When I get that into browser, it gets redirected to https://api.providersurl.com
(HTTPS!). Then, I'm able to authorize with the credentials.
The funny thing that kept me banging my head against the wall is that by accessing the HTTP URL, I did not get 404 (Not Found)
nor 302 (Redirect)
. I got 401 (Unauthorized)
. So I just kept trying! And boy, did I try...
When I changed the URL in the code to HTTPS, authorization worked with BOTH codes I posted.
It took a few hours... But no time is waste of time when you are learning a getting experienced.
精彩评论