发送网络请求时出现问题

发布于 2024-10-19 03:02:46 字数 2116 浏览 0 评论 0原文

我使用以下方法使用 HTTPWebRequest 从 Web 服务检索某些内容:

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);
            }


        }

现在我总是收到 401 - 访问被拒绝异常。我不知道为什么,因为如果我在网络浏览器中使用相同的 URL,它就会起作用。可能是因为括号的原因吗?

请注意:我在这里更改了服务器地址,因此它在这里不起作用,但出于保密原因我必须这样做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-10-26 03:02:46

您的缓存网址和请求网址不同,我认为这意味着您的用户名和密码没有在请求中传递。

String url = "http://123.123.123.123:8080/";

CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential("user", "pwd"));

使用 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)");

使用 abc.ch

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文