Reputation:
I use the following method to retrieve something from a webservice using a HTTPWebRequest:
private void RetrieveSourceCode(Method method)
{
try
{
String url = "http://123.123.123.123:8080/";
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential("user", "pwd"));
HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://abc.abc.ch:8080/famixParser/projects/argouml/org.argouml.uml.ui.behavior.common_behavior.ActionAddSendActionSignal.doIt(java.util.Collection)");
Console.WriteLine(request.RequestUri.ToString());
request.Credentials = myCache;
request.Accept = "text/plain";
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception e)
{
Console.WriteLine("exception when sending query: ");
throw e;
}
Stream resStream = response.GetResponseStream();
byte[] buf = new byte[8192];
StringBuilder sb = new StringBuilder();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
String sourceCode = sb.ToString();
method.setSourceCode(sourceCode);
Console.WriteLine(sourceCode);
request.Abort();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Now I always get a 401 - Access denied exception. I don't know why, because if i use the same URL in my webbrowser, it works. Is it maybe because of the parantheses?
Please note: I changed the server address here, so its not working here, but I had to do it for confidentiality reasons.
Upvotes: 5
Views: 604
Reputation: 1923
You cache url and request url are different, I would think that means your username and password aren't being passed in the request.
String url = "http://123.123.123.123:8080/";
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential("user", "pwd"));
uses 123.123
HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://abc.abc.ch:8080/famixParser/projects/argouml/org.argouml.uml.ui.behavior.common_behavior.ActionAddSendActionSignal.doIt(java.util.Collection)");
uses abc.ch
Upvotes: 2