使用 .NET 从 API 获取结果

发布于 2024-08-10 00:14:29 字数 366 浏览 2 评论 0原文

我想在我的 .NET 应用程序中使用 Rapidshare API,但我对如何发送请求并返回结果感到困惑。你使用Winsock还是其他方法?

网址是这样的: http://api. rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files=288725357&filenames=my_upload.txt

谢谢。

I wanted to use the Rapidshare API in my .NET app, but I am confused on how you send the request and bring back the result. Do you use Winsock or another method?

URLs are like this:
http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files=288725357&filenames=my_upload.txt

Thanks.

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

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

发布评论

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

评论(2

欲拥i 2024-08-17 00:14:29

查看 System.Net 命名空间,特别是 System.Net.WebClient。

http://msdn.microsoft.com /en-us/library/system.net.webclient(VS.80).aspx

Check out the System.Net namespace, specifically System.Net.WebClient.

http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx

战皆罪 2024-08-17 00:14:29

使用 WebClient 类。

http://msdn.microsoft。 com/en-us/library/system.net.webclient%28VS.80%29.aspx

您可以使用此类以编程方式与网页交互。以下是登录网站的一些示例代码。您可以对其进行调整以与他们的 Web API 进行交互:

HttpWebRequest request;
            HttpWebResponse response;
            CookieContainer cookies;
            string url = "http://www.jaxtr.com/user/login.jsp";

            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowAutoRedirect = true;
                request.CookieContainer = new CookieContainer();
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StringBuilder sb = new StringBuilder();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    while (!reader.EndOfStream)
                    {
                        sb.AppendLine(reader.ReadLine());
                    }

                    //Get the hidden value out of the form.                
                    String fp = Regex.Match(sb.ToString(), "\"__fp\"\\svalue=\"(([A-Za-z0-9+/=]){4}){1,19}\"", RegexOptions.None).Value;
                    fp = fp.Substring(14);
                    fp = fp.Replace("\"", String.Empty);


                    cookies = request.CookieContainer;
                    //response.Close();
                    String requestString = "http://www.jaxtr.com/user/Login.action?tzOffset=6&navigateURL=&refPage=&jaxtrId=" + HttpUtility.UrlEncode(credentials.Username) + "&password=" + HttpUtility.UrlEncode(credentials.Password) + "&Login=Login&_sourcePage=%2Flogin.jsp&__fp="+HttpUtility.UrlEncode(fp);
                    request = (HttpWebRequest)WebRequest.Create(requestString);
                    request.CookieContainer = cookies; //added by myself

                    response = (HttpWebResponse)request.GetResponse();
                    Console.WriteLine("Response from login:" + response.StatusCode);

                    String messageText = (message.TruncateMessage && message.MessageText.Length > JaxtrSmsMessage.MAX_MESSAGE_LENGTH ? message.MessageText.Substring(JaxtrSmsMessage.MAX_MESSAGE_LENGTH) : message.MessageText);

                    String messageURL = "http://www.jaxtr.com/user/sendsms?CountryName=" + HttpUtility.UrlEncode(message.CountryName) + "&phone=" + HttpUtility.UrlEncode(message.DestinationPhoneNumber) + "&message=" + HttpUtility.UrlEncode(messageText) + "&bySMS=" + HttpUtility.UrlEncode(message.BySMS.ToString().ToLower());

                    request = (HttpWebRequest)WebRequest.Create(messageURL);
                    request.CookieContainer = cookies;
                    response = (HttpWebResponse)request.GetResponse();

                    Console.WriteLine("Response from send SMS command=" + response.StatusCode);

                    StringBuilder output = new StringBuilder();

                    using (Stream s = response.GetResponseStream())
                    {
                        StreamReader sr = new StreamReader(s);
                        while (!sr.EndOfStream)
                        {
                            output.AppendLine(sr.ReadLine());
                        }
                    }
                    response.Close();
                }
                else
                {
                    Console.WriteLine("Client was unable to connect!");
                }
            }
            catch (System.Exception e)
            {
                throw new SMSDeliveryException("Unable to deliver SMS message because "+e.Message, e);
            }

此特定代码登录 Jaxtr(一种 SMS 消息传递服务)并发送 SMS 消息。

Use the WebClient Class.

http://msdn.microsoft.com/en-us/library/system.net.webclient%28VS.80%29.aspx

You can use this class to programatically interact with webpage. Here's some example code to log into a website. You can adapt this to interact with their web API:

HttpWebRequest request;
            HttpWebResponse response;
            CookieContainer cookies;
            string url = "http://www.jaxtr.com/user/login.jsp";

            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowAutoRedirect = true;
                request.CookieContainer = new CookieContainer();
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    StringBuilder sb = new StringBuilder();
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    while (!reader.EndOfStream)
                    {
                        sb.AppendLine(reader.ReadLine());
                    }

                    //Get the hidden value out of the form.                
                    String fp = Regex.Match(sb.ToString(), "\"__fp\"\\svalue=\"(([A-Za-z0-9+/=]){4}){1,19}\"", RegexOptions.None).Value;
                    fp = fp.Substring(14);
                    fp = fp.Replace("\"", String.Empty);


                    cookies = request.CookieContainer;
                    //response.Close();
                    String requestString = "http://www.jaxtr.com/user/Login.action?tzOffset=6&navigateURL=&refPage=&jaxtrId=" + HttpUtility.UrlEncode(credentials.Username) + "&password=" + HttpUtility.UrlEncode(credentials.Password) + "&Login=Login&_sourcePage=%2Flogin.jsp&__fp="+HttpUtility.UrlEncode(fp);
                    request = (HttpWebRequest)WebRequest.Create(requestString);
                    request.CookieContainer = cookies; //added by myself

                    response = (HttpWebResponse)request.GetResponse();
                    Console.WriteLine("Response from login:" + response.StatusCode);

                    String messageText = (message.TruncateMessage && message.MessageText.Length > JaxtrSmsMessage.MAX_MESSAGE_LENGTH ? message.MessageText.Substring(JaxtrSmsMessage.MAX_MESSAGE_LENGTH) : message.MessageText);

                    String messageURL = "http://www.jaxtr.com/user/sendsms?CountryName=" + HttpUtility.UrlEncode(message.CountryName) + "&phone=" + HttpUtility.UrlEncode(message.DestinationPhoneNumber) + "&message=" + HttpUtility.UrlEncode(messageText) + "&bySMS=" + HttpUtility.UrlEncode(message.BySMS.ToString().ToLower());

                    request = (HttpWebRequest)WebRequest.Create(messageURL);
                    request.CookieContainer = cookies;
                    response = (HttpWebResponse)request.GetResponse();

                    Console.WriteLine("Response from send SMS command=" + response.StatusCode);

                    StringBuilder output = new StringBuilder();

                    using (Stream s = response.GetResponseStream())
                    {
                        StreamReader sr = new StreamReader(s);
                        while (!sr.EndOfStream)
                        {
                            output.AppendLine(sr.ReadLine());
                        }
                    }
                    response.Close();
                }
                else
                {
                    Console.WriteLine("Client was unable to connect!");
                }
            }
            catch (System.Exception e)
            {
                throw new SMSDeliveryException("Unable to deliver SMS message because "+e.Message, e);
            }

This particular code logs into Jaxtr, a SMS messaging service, and sends an SMS message.

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