C# Web Reference and PHP - c#

I am attempting to call a Web Service (created in PHP) from my C# application. I have successfully added the Web Reference in Visual Studios, however I cannot figure out exactly how to invoke the call to the web service. I have been following this tutorial: http://sanity-free.org/article25.html however when I try and compile I get a "The type or namespace name 'SimpleService' could not be found". My C# code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace inVision_OCR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void translateButton_Click(object sender, EventArgs e)
{
// We need to snap the image here somehow . . .
// Open up the image and read its contents into a string
FileStream file = new FileStream("\\Hard Disk\\ocr_images\\test.jpg", FileMode.Open);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
sr.Close();
// Using SOAP, pass this message to our development server
SimpleService svc = new SimpleService();
string s1 = svc.getOCR("test");
MessageBox.Show(s);
}
}
}

The SimpleService is probably in a different namespace, look at the properties of the web reference you added and see what namespace the proxy class SimpleService is being generated in, then add a using statement in your code to reference that namespace.

Related

Google Vision API Document_Text_Detection

I am trying to develop C# Google Vision API function.
the code is supposed to compile into dll and it should run to do the following steps.
get the image from the image Path.
send the image to Google vision api
Call the document text detection function
get the return value (text string values)
Done
When I run the dll, However, it keeps giving me an throw exception error. I am assuming that the problem is on the google credential but not sure...
Could somebody help me out with this? I don't even know that the var credential = GoogleCredential.FromFile(Credential_Path); would be the right way to call the json file...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Vision.V1;
using Google.Apis.Auth.OAuth2;
using Image = Google.Cloud.Vision.V1.Image;
namespace DLL_TEST_NetFramework4._6._1version
{
public class Class1
{
public string doc_text_dection(string GVA_File_Path, string Credential_Path)
{
var credential = GoogleCredential.FromFile(Credential_Path);
//Load the image file into memory
var image = Image.FromFile(GVA_File_Path);
// Instantiates a client
ImageAnnotatorClient client = ImageAnnotatorClient.Create();
TextAnnotation text = client.DetectDocumentText(image);
//Console.WriteLine($"Text: {text.Text}");
return $"Text: {text.Text}";
//return "test image...";
}
}
}
You just need to setup the environment variable GOOGLE_APPLICATION_CREDENTIALS as mentioned here
You mus have to mention you json file name in the environment variable as this.
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Your_Json_File_Name.json");
Your code would look like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Vision.V1;
using Google.Apis.Auth.OAuth2;
using Image = Google.Cloud.Vision.V1.Image;
namespace DLL_TEST_NetFramework4._6._1version
{
public class Class1
{
public string doc_text_dection(string GVA_File_Path, string Credential_Path)
{
//var credential = GoogleCredential.FromFile(Credential_Path);
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "Your_Json_File_Name.json");
//Load the image file into memory
var image = Image.FromFile(GVA_File_Path);
// Instantiates a client
ImageAnnotatorClient client = ImageAnnotatorClient.Create();
TextAnnotation text = client.DetectDocumentText(image);
//Console.WriteLine($"Text: {text.Text}");
return $"Text: {text.Text}";
//return "test image...";
}
}
}
or you can send it through your Credential_Path variable.
for more details please visit Google Vision API Docs
You need to setup your environment in your console with code like this :
Windows Server:
$env:GOOGLE_APPLICATION_CREDENTIALS="File Path"
Linux Server :
export GOOGLE_APPLICATION_CREDENTIALS="File Path"
Hope it helps!

this api script for a app for a site i made is not working

I need help with this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace kbam_.API
{
class filea
{
public static string filea(string url) //this code right here
{
string contents;
var wc = new System.Net.WebClient();
contents = wc.DownloadString(url);
}
}
}
and yes I am authorized by kesbook uk to use this I'm the owner so I can use what I want http://kesbook.cf/autho
If you are querying a a RESTful API I would suggest using the Restsharp nugget package, it is much easier to use.

Testing a web service by returning a string of data in a web application form

I've got a web service but need to know how to test it in a windows application form.
here is the start of the service. Would I put the form code inside a button, or just return it in a label? Not exactly overly clued into c# or .net I've called the web service succesfully thougha nd just need to return the string to make sure encryption is working.
<%# WebService Language="C#" Class="UserEncryptionLink.EncryptUserLink" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Text;
using System.Security.Cryptography;
namespace UserEncryptionLink
{
/// <summary>
/// This Web Service is to encrypt user details and display them in the URL when they click on a link taking them to InfoExchange
/// </summary>
[WebService(Namespace = " Webspace name")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class EncryptUserLink : System.Web.Services.WebService
{
[WebMethod]
public void TestCypher()
{
var key = "12345";
var vector = "12345";
var username = "YOURDOMAIN\\YOURUSERNAME";
var url = "sitename.com";
var it = GetSingleSignOnUrl(url, username, key, vector);
}
And my form
Yeah I've got my form referencing the service, it looks like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FormForEncrypt.nameofURL;
using System.Net;
namespace FormForEncrypt
{
public partial class EncryptForm : Form
{
public EncryptForm()
{
InitializeComponent();
}
// creates an instance of the web service as a member of the class, will put it down below, doesn't seem to work though declared in the using statement
private nameofURL.EncryptUserLink userform = new nameofUrl.EncryptUserLink();
[System.Web.Services.WebMethod]
private void testButton_Click_1(object sender, EventArgs e)
{
//method to send on button click, then recieve the string to show it works, it should come out as http://CLIENTNAME.info-exchange.com/yyyyMMddHHmmssDomainUsername
//create instances of the details
string[] it;
//send string
//recieve string and display, it must display the same as what was sent.
}
}
}
Not sure what you mean if you should put the form code inside a button or return it in a label?
Webservices don't have concepts of forms or label or anything. They are just services... no forms, no controls... just somewhat "raw code" waiting to be served up by invoking it and your hosting protocol (such as IIS or WAS or self hosting app) takes care of spinning it up and running the service.
interms of calling it... well, it depends where you want to call it from your application. Could be on a button click, could be on a timer, could be on a form load... depends on you and your application logic.
your current web method, TestChypher, does not return anything at the moment. you need to, I am guessing, return a bool value to indicate if the string matches or not?
again, depends on you and what you want to return. if you wanted to return a string, change the method signature to return a string - so change "void" to "string" and return back, in that method, a string you want to return back.
example - short snip:
[WebMethod]
public void SayHello()
{
return "Hello";
}

LinkFinder.find fails to work in webcrawler app

Wrote code as the start to a web crawler that scrapes links from webpage.
Following the instructions from this page:
http://www.dotnetperls.com/scraping-html
I seem to get an error that LinkFinder cannot be found?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace WebCrawler
{
class Program
{
static void Main(string[] args)
{
WebClient url = new WebClient();
String initialLink = url.DownloadString("http://www.FAKEADDRESS.org.uk/");
for (LinkItem i in LinkFinder.find(initialLink))
{
System.Diagnostics.Debug.WriteLine(initialLink);
}
}
}
}
LinkFinder is a class that is included in the code at that URL you provided. Make sure you also copy that class into your project in some way (a file by itself, in another file, whatever).

Im getting exception Typeload what the exception can be?

I added as reference 3 dll's: Google.Apis , Google.Apis.Translate.v2 , System.Runtime.Serialization
In Form1 i have one line:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Translator.translate(new TranslateInput());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Now the error the exception is on the first line in the class Translator:
The line that throw the error is: var service = new TranslateService { Key = GetApiKey() };
The class code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using Google.Apis.Util;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;
public class Translator
{
public static string translate(TranslateInput input)
{
// Create the service.
var service = new TranslateService { Key = GetApiKey() };
string translationResult = "";
// Execute the first translation request.
Console.WriteLine("Translating to '" + input.TargetLanguage + "' ...");
TranslationsListResponse response = service.Translations.List(input.SourceText, input.TargetLanguage).Fetch();
var translations = new List<string>();
foreach (TranslationsResource translation in response.Translations)
{
translationResult = translation.TranslatedText;
}
return translationResult;
}
private static string GetApiKey()
{
return "AIzaSyCjxMe6RKHZzd7xSfSh2pEsBqUdXYm5tA8"; // Enter Your Key
}
}
/// <summary>
/// User input for this example.
/// </summary>
[Description("input")]
public class TranslateInput
{
[Description("text to translate")]
public string SourceText = "Who ate my candy?";
[Description("target language")]
public string TargetLanguage = "fr";
}
The error is:
Could not load type 'Google.Apis.Discovery.FactoryParameterV1_0' from assembly 'Google.Apis, Version=1.1.4497.35846, Culture=neutral, PublicKeyToken=null'.
Tried to google for help and also tried to change the project type to x64 platform but it didnt help. So i put it back on x86
I have windows 7 64bit visual studio c# 2010 pro .net 4.0 profile client.
Cant figure out what is the error ?
This error as reported in the above-posted messages is due to a local copy in the bin\Debug folder of your solution or project. Even though you attempt to clean your solution, such copies will persist to exist.
In order to avoid this to happen, you have to force Visual Studio to refer to the correct DLL by adding reference paths within a project properties. Unfortunately, if you got several projects within your solutions, you will have to set the reference paths for the projects one after another until completed.
Should you wish to know how to setup reference paths follow these simple instructions:
1.Select your project, right-click, then click "Properties";
2.In the project properties, click "Reference Paths";
3.Folder, type or browse to the right location of your DLL, click [Add Folder].
You will need to perform these steps for as many different locations you may have for each of your DLLs. Consider setting an output path under the Build tab of the same project properties, so that you may output your DLLs in the same directory for each of them, thus assuring you to find all the latest builds under the same location, simplifying forward your referencing.
Note this can only be one reason for this error. But it is sure that is has to do something with a wrong copy of the mentioned assembly.

Categories

Resources