Home .NET pass a variable to the second block of code
Reply: 2

.NET pass a variable to the second block of code

liamcook
1#
liamcook Published in 2018-01-12 15:07:48Z

Very simple question though I'm pretty baffled, I found myself using this function a lot in my controllers so moved it to a method which is my first one. The foreach loop has a final string FinalURL how do I pass that into the httpWebRequest? do I make another method and call it after the break? Before i used a viewbag variable but in this context i cant use it. If i remove the brackets it says not all paths return a function and i believe this isn't the correct way.

namespace Linkofy.Models
{
public class MajesticFunctions
{
    public static int[] MajesticChecker(string[] URLtests)
    {
        foreach (string URLt in URLtests)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URLt);
            myHttpWebRequest.AllowAutoRedirect = false;
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            int resulting = (int)myHttpWebResponse.StatusCode;
            if (resulting == 200)
            {
                String Urlnew = URLt;
                String FinalURL = URLt.Replace("https://", "").Replace("http://", "");
                break;
            }
        }
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.majestic.com/api/json?app_api_key=KEY&cmd=GetIndexItemInfo&items=1&item0=" + FinalURL + "&datasource=fresh");
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                JObject jObject = JObject.Parse(reader.ReadToEnd());
                JToken Trusty = jObject["DataTables"]["Results"]["Data"][0]["TrustFlow"].Value<int>();
                JToken City = jObject["DataTables"]["Results"]["Data"][0]["CitationFlow"].Value<int>();
                JToken RIPy = jObject["DataTables"]["Results"]["Data"][0]["RefIPs"].Value<int>();

                int Trustflow = Int32.Parse(Trusty.ToString());
                int Citationflow = Int32.Parse(City.ToString());
                int Reffering = Int32.Parse(RIPy.ToString());

                int[] Metrics = new int[] { Trustflow, Citationflow, Reffering };

                return Metrics;
            }
        }
    }
}

}

Ctznkane525
2#
Ctznkane525 Reply to 2018-01-12 15:49:06Z

Put it on the outside of the loop.

That way it will be available in the scope outside of the loop.

String FinalURL = "";
foreach (string URLt in URLtests)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URLt);
            myHttpWebRequest.AllowAutoRedirect = false;
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            int resulting = (int)myHttpWebResponse.StatusCode;
            if (resulting == 200)
            {
                String Urlnew = URLt;
                FinalURL = URLt.Replace("https://", "").Replace("http://", "");
                break;
            }
        }

if ( FinalURL.Length > 0 )
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.majestic.com/api/json?app_api_key=KEY&cmd=GetIndexItemInfo&items=1&item0=" + FinalURL + "&datasource=fresh");
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                JObject jObject = JObject.Parse(reader.ReadToEnd());
                JToken Trusty = jObject["DataTables"]["Results"]["Data"][0]["TrustFlow"].Value<int>();
                JToken City = jObject["DataTables"]["Results"]["Data"][0]["CitationFlow"].Value<int>();
                JToken RIPy = jObject["DataTables"]["Results"]["Data"][0]["RefIPs"].Value<int>();

                int Trustflow = Int32.Parse(Trusty.ToString());
                int Citationflow = Int32.Parse(City.ToString());
                int Reffering = Int32.Parse(RIPy.ToString());

                int[] Metrics = new int[] { Trustflow, Citationflow, Reffering };

                return Metrics;
            }
        }
}
else
{
    throw new Exception("No Final URL Found");
}
Surjit SD
3#
Surjit SD Reply to 2018-01-12 16:32:11Z

You can also move you WebRequest code in for loop

namespace Linkofy.Models
{
public class MajesticFunctions
{
    public static int[] MajesticChecker(string[] URLtests)
    {
        foreach (string URLt in URLtests)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URLt);
            myHttpWebRequest.AllowAutoRedirect = false;
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            int resulting = (int)myHttpWebResponse.StatusCode;
            if (resulting == 200)
            {

                String FinalURL = URLt.Replace("https://", "").Replace("http://", "");
                if (FinalURL.Length > 0)
                {
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.majestic.com/api/json?app_api_key=KEY&cmd=GetIndexItemInfo&items=1&item0=" + FinalURL + "&datasource=fresh");
                  {
                     WebResponse response = request.GetResponse();
                     using (Stream responseStream = response.GetResponseStream())
                     {
                         StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                         JObject jObject = JObject.Parse(reader.ReadToEnd());
                         JToken Trusty = jObject["DataTables"]["Results"]["Data"][0]["TrustFlow"].Value<int>();
                         JToken City = jObject["DataTables"]["Results"]["Data"][0]["CitationFlow"].Value<int>();
                         JToken RIPy = jObject["DataTables"]["Results"]["Data"][0]["RefIPs"].Value<int>();

                         int Trustflow = Int32.Parse(Trusty.ToString());
                         int Citationflow = Int32.Parse(City.ToString());
                         int Reffering = Int32.Parse(RIPy.ToString());

                         int[] Metrics = new int[] { Trustflow, Citationflow, Reffering };

                         return Metrics;
                      }
                  }
                }
                break;
            }
        }
        throw new Exception("No Final URL Found");

       }
}
}

Sorry for bad formatting

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331947 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO